Message history
Overview¶
Each session keeps its conversation in a MessageHistory. For the agent's
default session it is available as agent.conversation. It provides:
- the messages of the conversation (
ChatMessageobjects) - context messages added outside of runs
- loading earlier conversations from storage
- temporary history changes (
temporary_state())
Managing History¶
history = agent.conversation
messages = history.get_history() # all messages
history.set_history(messages[-10:]) # replace the history
await history.clear() # start over
# Run with a temporary history, restored afterwards
async with history.temporary_state(["Earlier context"], replace_history=True):
await agent.run("A question without the rest of the conversation")
Context¶
Context is added to the conversation without running the agent:
history.add_context_message(
"Important background info",
source="documentation",
type="background", # further keyword arguments are stored as metadata
)
await history.load_context_source("docs/api.md") # a file or URL
# Format the history as text, for example for another agent
text = await history.format_history(max_tokens=1000, num_messages=5)
Loading Earlier Conversations¶
The session field of a native agent loads a stored conversation when the agent starts,
by name or by a query (see Session configuration):
agents:
my_agent:
model: openai:gpt-5
session:
name: support_chat # Session identifier
since: 1h # Time period to load
until: 5m # Up to this time ago
agents: [support] # Only messages of these agents
roles: [user, assistant] # Only these roles
contains: "error" # Text search
limit: 50 # Max messages
The same works in Python:
from agentpool import Agent
from agentpool_config.session import SessionQuery
async def main():
async with Agent(
model="openai:gpt-5-mini",
session=SessionQuery(name="support_chat", since="1h"),
) as agent:
...
The stored history of an agent is available with await agent.get_message_history(limit=50).
Compaction¶
Long conversations can be compacted: await agent.compact() shrinks the history of a session
(filtering, truncating or summarizing messages). The steps are configured in the manifest's
compaction section:
CompactionConfig¶
Configuration for message compaction/summarization.
Example YAML:
compaction:
steps:
- type: filter_thinking
- type: truncate_tool_outputs
max_length: 1000
- type: keep_last
count: 10
Or use a preset:
FilterThinkingConfig¶
Configuration for FilterThinking step.
FilterRetryPromptsConfig¶
Configuration for FilterRetryPrompts step.
FilterBinaryContentConfig¶
Configuration for FilterBinaryContent step.
FilterToolCallsConfig¶
Configuration for FilterToolCalls step.
FilterEmptyMessagesConfig¶
Configuration for FilterEmptyMessages step.
TruncateToolOutputsConfig¶
Configuration for TruncateToolOutputs step.
| TruncateToolOutputsConfig (YAML) | |
|---|---|
TruncateTextPartsConfig¶
Configuration for TruncateTextParts step.
| TruncateTextPartsConfig (YAML) | |
|---|---|
KeepLastMessagesConfig¶
Configuration for KeepLastMessages step.
KeepFirstMessagesConfig¶
Configuration for KeepFirstMessages step.
KeepFirstAndLastConfig¶
Configuration for KeepFirstAndLast step.
TokenBudgetConfig¶
Configuration for TokenBudget step.
SummarizeConfig¶
Configuration for Summarize step.
| SummarizeConfig (YAML) | |
|---|---|
WhenMessageCountExceedsConfig¶
Configuration for WhenMessageCountExceeds wrapper.