Context
Choose what the policy sees at each step. ContextManager owns prompt preparation and context changes; this module supplies transcript retention, exact token continuation, summarization, and their triggers.
ContextManager
Section titled “ContextManager”Decide what the policy sees. A context manager renders each prompt and, when its trigger fires, compacts the live context. The agent records each compaction’s turns, effect, and context version.
This manager restarts from the system prompt and task, plus the last four messages, once the prompt exceeds 4,096 tokens. The agent records the fold and advances the context version. Context managers are shared across branches; keep branch-specific data in the rollout state. A manager can also set the agent’s defaults: max_tokens caps each request, final_answer=False ends a rollout out of steps without a final-answer turn, branch_points() chooses where search may continue, action declares the action kind its decisions need, and check_policy() refuses an incompatible policy at construction. Segments sets all five.
Definition
Fields
ContextManager.manages_context
Section titled “ContextManager.manages_context”Whether this manager ever changes the live context. AppendOnly returns False.
ContextManager.branch_points
Section titled “ContextManager.branch_points”Where search may continue when the agent is given no branch_points. The default is SparseCheckpoints(); Segments returns EveryDecision().
ContextManager.check_policy
Section titled “ContextManager.check_policy”Raise if this manager cannot render prompts for policy. The agent calls it once, at construction.
ContextManager.should_compact
Section titled “ContextManager.should_compact”Whether to compact before the next decision. The default asks trigger.
ContextManager.compact
Section titled “ContextManager.compact”Return the compacted context and any turns generated while producing it.
ContextManager.prepare
Section titled “ContextManager.prepare”Render the prompt for the next generation. The default calls the policy’s prepare() with the conversation and tool schemas.
ContextManager.record_reply
Section titled “ContextManager.record_reply”Append a model reply to the branch’s messages. The default delegates to the policy.
ContextManager.schemas
Section titled “ContextManager.schemas”Schemas for context tools the model may call. The default offers none.
ContextManager.initialize
Section titled “ContextManager.initialize”Prepare a new rollout’s state for this manager, for example by checking that the task state carries the counters the trigger reads.
ContextManager.apply
Section titled “ContextManager.apply”Apply a recorded action and return the updated rollout and its transition. The default delegates to the environment’s step(), or finish() on the final-answer turn.
AppendOnly
Section titled “AppendOnly”Keep the full context between requests: the policy sees the whole conversation, including tool results. This is the agent’s default context manager.
The agent renders the accumulated conversation for every decision and never compacts it. Use Segments(tokens) to continue one assistant response in exact token segments, or Summarize(...) to fold a long transcript. The context manager determines what the policy sees; branch_points separately determines where search may continue.
Definition
AppendOnly.init
Section titled “AppendOnly.init”AppendOnly.check_policy
Section titled “AppendOnly.check_policy”AppendOnly.initialize
Section titled “AppendOnly.initialize”In continuation mode, check that the policy accepts exact token input before the rollout starts.
AppendOnly.prepare
Section titled “AppendOnly.prepare”Prepare this branch’s next model input. The default renders the full conversation; with continuation=True, as Segments uses, it appends the original generated tokens to the initial prompt and marks the request for exact-continuation validation.
AppendOnly.record_reply
Section titled “AppendOnly.record_reply”Record the assistant reply. In continuation mode, keep its original token IDs in branch-local message metadata.
Segments
Section titled “Segments”Continue one assistant response in exact segments of at most tokens tokens. Each segment is a decision, so search can branch in the middle of a long answer.
Segments(256) caps each request at 256 tokens, exposes each segment boundary for branching, and disables the extra final-answer request by default. It declares token-chunk actions, so the agent normally selects TokenChunkParser; an explicit Agent(parser=...) takes precedence. The policy must accept and return exact token IDs.
Definition
Segments.init
Section titled “Segments.init”Segments.branch_points
Section titled “Segments.branch_points”Summarize
Section titled “Summarize”Ask the policy to summarize the transcript, then continue the task from its notes. Summarization runs as a short sub-rollout with the same model and can use workspace tools.
This manager requests a summary when the rendered prompt exceeds 8,192 tokens. The summary task allows four interactions, with at most 1,024 generated tokens per reply. Use trigger= for another condition, such as StateCounter() after a round of tool calls. Task state and workspace survive compaction. Compaction consumes one outer agent step; its generated turns remain in the history for training. penalty_cap bounds the episode’s total penalty for malformed structured summaries.
Definition
Summarize.init
Section titled “Summarize.init”Summarize.compact
Section titled “Summarize.compact”Run the fold sub-rollout and return the new messages and its generated turns. Raises IncompleteCompactionError when the model does not produce usable notes.
Request compaction when any of the supplied triggers fires. After compaction, reset the state tracked by each trigger.
This example compacts when the prompt exceeds 8,192 tokens or the round’s tool-call allowance is spent. The task state must contain calls_remaining and a positive calls_max.
Definition
AnyOf.init
Section titled “AnyOf.init”AnyOf.of
Section titled “AnyOf.of”Construct the combined trigger from a sequence. Configuration mappings use this factory to build the entries in their triggers list.
AnyOf.fires
Section titled “AnyOf.fires”AnyOf.check
Section titled “AnyOf.check”Validate the task-state fields required by every trigger.
AnyOf.relieve
Section titled “AnyOf.relieve”Compaction
Section titled “Compaction”What a context manager’s compact() receives: the live messages, the seed conversation, the task state, the workspace, and the agent being folded.
Definition
Fields
CompactionResult
Section titled “CompactionResult”Return the retained messages and state produced by one compaction operation.
Definition
Fields
ContextError
Section titled “ContextError”An invalid context operation. The message is returned to the model as corrective feedback.
Definition
ContextTools
Section titled “ContextTools”Context operations the model calls as tools, such as dropping or summarizing earlier messages. A reply that mixes context operations with task actions is refused.
Definition
ContextTools.manages_context
Section titled “ContextTools.manages_context”Context tools always manage the live context.
ContextTools.operation
Section titled “ContextTools.operation”Apply one named context operation to the rollout. Raise ContextError with a message the model can act on.
ContextTools.apply
Section titled “ContextTools.apply”Route context tool calls to operation() and other actions to the environment.
IncompleteCompactionError
Section titled “IncompleteCompactionError”Report rejected compaction output while retaining the generations that produced it.
Definition
IncompleteCompactionError.init
Section titled “IncompleteCompactionError.init”PromptTokens
Section titled “PromptTokens”Request compaction when the rendered prompt grows beyond a token threshold.
Fires once the prompt contains more than 8,192 tokens. Pass it as Summarize(trigger=trigger), or combine triggers with |.
Definition
PromptTokens.init
Section titled “PromptTokens.init”PromptTokens.fires
Section titled “PromptTokens.fires”StateCounter
Section titled “StateCounter”Request compaction when a task-state counter is spent, then refill that counter from its configured maximum.
Uses the fields supplied by RoundBudget. Initialize calls_max to a positive round allowance and charge calls_remaining as tool calls run.
Definition
StateCounter.init
Section titled “StateCounter.init”StateCounter.fires
Section titled “StateCounter.fires”StateCounter.check
Section titled “StateCounter.check”Check that the state contains both counter fields and that its refill allowance is positive. The agent performs this validation before generation.
StateCounter.relieve
Section titled “StateCounter.relieve”Trigger
Section titled “Trigger”Define when context compaction should happen. Implement fires and, for state-based budgets, relieve so the task can make progress after compaction.
Definition
Trigger.fires
Section titled “Trigger.fires”Required implementation.
Return whether the prompt or task state requires compaction.
Trigger.check
Section titled “Trigger.check”Validate the initial task state before generation. Override this hook when the trigger requires particular state fields. The default performs no checks.
Trigger.relieve
Section titled “Trigger.relieve”Return the task state to use after compaction. The default leaves it unchanged. Counter-based triggers reset their counters here so the same condition does not immediately trigger another compaction.
Applied
Section titled “Applied”A context manager’s updated rollout and optional task transition after applying an action. Context operations can update the rollout without advancing the task.
Definition
Fields
DEFAULT_INSTRUCTION
Section titled “DEFAULT_INSTRUCTION”Provide the default instruction given to the agent during context compaction.
Definition