Skip to content

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 (ChatMessage objects)
  • 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:

compaction:
  preset: balanced  # or: minimal, summarizing

agents:
  assistant:
    model: openai:gpt-5-mini

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:

compaction:
  preset: balanced

CompactionConfig (YAML)
1
2
3
compaction:
  preset: null  # Use a predefined compaction pipeline.
  steps: []  # Ordered list of compaction steps to apply (ignored if preset is set).

FilterThinkingConfig

Configuration for FilterThinking step.

FilterThinkingConfig (YAML)
steps:
- type: filter_thinking

FilterRetryPromptsConfig

Configuration for FilterRetryPrompts step.

FilterRetryPromptsConfig (YAML)
steps:
- type: filter_retry_prompts

FilterBinaryContentConfig

Configuration for FilterBinaryContent step.

FilterBinaryContentConfig (YAML)
1
2
3
steps:
- type: filter_binary
  keep_references: false

FilterToolCallsConfig

Configuration for FilterToolCalls step.

FilterToolCallsConfig (YAML)
1
2
3
4
steps:
- type: filter_tools
  exclude_tools: []
  include_only: null

FilterEmptyMessagesConfig

Configuration for FilterEmptyMessages step.

FilterEmptyMessagesConfig (YAML)
steps:
- type: filter_empty

TruncateToolOutputsConfig

Configuration for TruncateToolOutputs step.

TruncateToolOutputsConfig (YAML)
1
2
3
4
5
6
steps:
- type: truncate_tool_outputs
  max_length: 2000
  suffix: '

    ... [truncated]'

TruncateTextPartsConfig

Configuration for TruncateTextParts step.

TruncateTextPartsConfig (YAML)
1
2
3
4
5
6
steps:
- type: truncate_text
  max_length: 5000
  suffix: '

    ... [truncated]'

KeepLastMessagesConfig

Configuration for KeepLastMessages step.

KeepLastMessagesConfig (YAML)
1
2
3
4
steps:
- type: keep_last
  count: 10
  count_pairs: true

KeepFirstMessagesConfig

Configuration for KeepFirstMessages step.

KeepFirstMessagesConfig (YAML)
1
2
3
steps:
- type: keep_first
  count: 2

KeepFirstAndLastConfig

Configuration for KeepFirstAndLast step.

KeepFirstAndLastConfig (YAML)
1
2
3
4
steps:
- type: keep_first_last
  first_count: 2
  last_count: 5

TokenBudgetConfig

Configuration for TokenBudget step.

TokenBudgetConfig (YAML)
1
2
3
4
steps:
- type: token_budget
  max_tokens: 4000
  model: gpt-4o

SummarizeConfig

Configuration for Summarize step.

SummarizeConfig (YAML)
1
2
3
4
5
6
steps:
- type: summarize
  model: openai:gpt-4o-mini
  threshold: 15
  keep_recent: 5
  summary_prompt: null

WhenMessageCountExceedsConfig

Configuration for WhenMessageCountExceeds wrapper.

WhenMessageCountExceedsConfig (YAML)
1
2
3
4
steps:
- type: when_count_exceeds
  threshold: 20
  step: a