Context Engineering
Context engineering (aka “memory management” or “context management”) is the process of managing the context window of an agent to ensure it has access to the information it needs to perform its task.
Letta and MemGPT introduced the concept of agentic context engineering, where the context window engineering is done by one or more AI agents. In Letta, agents are able to manage their own context window (and the context window of other agents!) using special memory management tools.
Memory management in regular agents
By default, Letta agents are provided with tools to modify their own memory blocks. This allows agents to learn and form memories over time, as described in the MemGPT paper.
The default tools are:
core_memory_replace
: Replace a value inside a blockcore_memory_append
: Append a new value to a block
If you do not want your agents to manage their memory, you should disable default tools with include_base_tools=False
during the agent creation. You can also detach the memory editing tools post-agent creation - if you do so, remember to check the system instructions to make sure there are no references to tools that no longer exist.
Memory management with sleep-time compute
If you want to enable memory management with sleep-time compute, you can set enable_sleeptime=True
in the agent creation. For agents enabled with sleep-time, Letta will automatically create sleep-time agents which have the ability to update the blocks of the primary agent.
Memory management with sleep-time compute can reduce the latency of your main agent (since it is no longer responsible for managing its own memory), but can come at the cost of higher token usage. See our documentation on sleeptime agents for more details.
Enabling agents to modify their own memory blocks with tools
You can enable agents to modify their own blocks with tools. By default, agents with type memgpt_agent
will have the tools core_memory_replace
and core_memory_append
to allow them to replace or append values in their own blocks. You can also make custom modification to blocks by implementing your own custom tools that can access the agent’s state by passing in the special agent_state
parameter into your tools.
Below is an example of a tool that re-writes the entire memory block of an agent with a new string:
Modifying blocks via the API
You can also modify blocks via the API to directly edit agents’ context windows and memory. This can be useful in cases where you want to extract the contents of an agents memory some place in your application (for example, a dashboard or memory viewer), or when you want to programatically modify an agents memory state (for example, allowing an end-user to directly correct or modify their agent’s memory).
Modifying blocks of other Letta agents via API tools
Importing the Letta Python client inside a tool is a powerful way to allow agents to interact with other agents, since you can use any of the API endpoints. For example, you could create a custom tool that allows an agent to create another Letta agent.
You can allow agents to modify the blocks of other agents by creating tools that import the Letta Python SDK, then using the block update endpoint: