Context Engineering

How Letta engineerings the context window of your agents

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 block
  • core_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:

1def rethink_memory(agent_state: "AgentState", new_memory: str, target_block_label: str) -> None:
2 """
3 Rewrite memory block for the main agent, new_memory should contain all current information from the block that is not outdated or inconsistent, integrating any new information, resulting in a new memory block that is organized, readable, and comprehensive.
4
5 Args:
6 new_memory (str): The new memory with information integrated from the memory block. If there is no new information, then this should be the same as the content in the source block.
7 target_block_label (str): The name of the block to write to.
8
9 Returns:
10 None: None is always returned as this function does not produce a response.
11 """
12
13 if agent_state.memory.get_block(target_block_label) is None:
14 agent_state.memory.create_block(label=target_block_label, value=new_memory)
15
16 agent_state.memory.update_block_value(label=target_block_label, value=new_memory)
17 return None

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:

1def update_supervisor_block(block_label: str, new_value: str) -> None:
2 """
3 Update the value of a block in the supervisor agent.
4
5 Args:
6 block_label (str): The label of the block to update.
7 new_value (str): The new value for the block.
8
9 Returns:
10 None: None is always returned as this function does not produce a response.
11 """
12 from letta_client import Letta
13
14 client = Letta(
15 base_url="http://localhost:8283"
16 )
17
18 client.agents.blocks.modify(
19 agent_id=agent_id,
20 block_label=block_label,
21 value=new_value
22 )