TNS
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
NEW! Try Stackie AI
AI / AI Engineering / Large Language Models

Enhancing AI Agents: Adding Instructions, Tasks and Memory

We show you three crucial enhancements to make your AI agents more sophisticated and effective: instructions, tasks, and conversation memory.
Nov 17th, 2024 6:00am by
Featued image for: Enhancing AI Agents: Adding Instructions, Tasks and Memory
Image via Unsplash+. 

In our previous article on AI agents, we explored how to create a basic AI agent with a persona using system prompts. Now, we’ll delve into three crucial enhancements that make our agents more sophisticated and effective: instructions, tasks, and conversation memory.

Understanding Instructions vs. Persona

While a persona defines an agent’s core traits and role (like being a financial advisor), instructions provide specific guidelines for how the agent should operate. Think of the persona as like an employee’s job title and basic qualifications, while instructions are like the detailed operational manual they follow.

Let’s look at how instructions are implemented in our enhanced agent class:


The separation between persona and instruction allows for more flexible and precise control over the agent’s behavior. For example, a financial advisor agent might have:

  • Persona: Core identity as a financial expert.
  • Instructions: Specific guidelines about never recommending individual stocks, always including disclaimers, etc.

Understanding Tasks

While personas define the role and instructions provide operational guidelines, tasks represent the specific queries or actions requested of the agent.


In our implementation, tasks are handled distinctly from instructions, allowing for clearer separation of concerns:

  • Instructions: Long-term guidelines that persist across conversations.
  • Tasks: Immediate requests that need to be addressed.

Tasks work together with instructions to form the complete user-side context in the prompt structure. Here’s how they combine in the message chain:


This structure ensures that:

  • Instructions provide the operational framework.
  • Tasks represent the immediate objective.
  • The agent maintains awareness of both the general guidelines and specific requests.

Task Processing Flow

When a task is executed, the following process takes place:

  • The agent first considers its persona (system role).
  • The agent then applies any global instructions.
  • Finally, it processes the specific task.
  • The response is stored in the conversation history.

This layered approach allows the agent to:

  • Maintain its professional identity (persona).
  • Follow consistent guidelines (instructions).
  • Address specific needs (tasks).
  • Build context over time (memory).

When to Use Instructions vs. Tasks

Understanding the distinction between instructions and tasks is fundamental to designing effective AI agents. Instructions serve as the foundational framework that guides an agent’s behavior across all interactions. Think of instructions as the operating manual for the agent — they define broad behavioral guidelines, establish persistent rules and boundaries, and set methodologies for handling various situations. Like a company’s standard operating procedures, instructions remain consistent across multiple interactions, ensuring the agent maintains reliable and has predictable behavior patterns.

While instructions provide the overarching framework, tasks are the practical implementation of that framework in response to specific user requests.

Tasks, on the other hand, represent the specific actions an agent needs to perform at any given moment. They are the concrete, immediate objectives that change based on user needs and requirements. While instructions provide the overarching framework, tasks are the practical implementation of that framework in response to specific user requests. Tasks must always operate within the boundaries set by the instructions, much like how an employee performs specific assignments while adhering to company policies.

Building Conversation Memory

One of the most significant enhancements is the addition of conversation memory. This allows our agent to maintain context throughout an interaction, much like how a human professional would remember previous parts of a conversation with a client.

The implementation uses a history property to track the conversation history:


The history, which acts as a short-term memory system, serves several purposes:

  • Contextual Understanding: The agent can reference previous interactions.
  • Consistency: Responses remain coherent across multiple exchanges.
  • Learning: The agent can build upon previous discussions.

Constructing Message Chains

The enhanced agent uses a sophisticated message-building system that combines persona, instructions, and conversation history.


This structured approach ensures that:

  • The persona remains the foundational context.
  • Instructions are clearly acknowledged.
  • Previous conversations provide additional context.
  • The current task is properly framed within the larger context.

Practical Implementation

Let’s look at a practical example using a financial advisor agent:


The agent now maintains context across multiple interactions while following specific guidelines:


In this example, the second response would take into account the context from the first interaction, providing more relevant and connected advice.

The entire source code for the framework is available in a GitHub repository.

Benefits of Enhanced Agents

These enhancements bring several key advantages:

  • Improved Consistency: The combination of persona and instructions ensures that responses are consistently aligned with the agent’s role and guidelines.
  • Better Context Management: Conversation memory allows for more natural and coherent interactions across multiple exchanges.
  • Flexible Behavior Control: Separating a persona from instructions makes it easier to modify how the agent operates without changing its core identity.
  • Enhanced User Experience: Users receive more contextually appropriate responses that build upon previous interactions.

Limitations and Next Steps

These enhancements represent a significant step forward in creating more sophisticated AI agents. However, there’s still room for further improvements, such as:

  • Long-term memory storage and retrieval;
  • Task-specific instruction sets;
  • Dynamic persona adaptation based on user needs and
  • Enhanced error handling and edge case management.

By continuing to refine and enhance these components, we can create AI agents that are increasingly capable of handling complex, multi-step interactions while maintaining consistency and contextual awareness.

The combination of persona, instructions, and memory brings us closer to creating AI agents that can truly serve as effective digital assistants across a wide range of professional domains.

In the next part of this series, we will add reasoning to agents based on proven prompt engineering techniques. Stay tuned!

Group Created with Sketch.
TNS owner Insight Partners is an investor in: Persona, OpenAI.
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.