This document explains how Codex maintains and manipulates the conversation history that forms the model's context window. It covers the ContextManager data structure, item recording and normalization, token estimation, and history lifecycle management.
For details on summarizing and compacting conversation history when context limits are approached, see History Compaction System. For information on persisting history to disk and replaying it during session resumption, see Rollout Persistence and Replay.
The conversation history manager is responsible for:
ResponseItem entries (user messages, assistant messages, tool calls, tool outputs, reasoning items, and compaction summaries) codex-rs/core/src/context_manager/history.rs43The primary entry point is the ContextManager struct, which is utilized by the SessionState to manage the transcript of a thread codex-rs/core/src/state/session.rs26-28
Sources: codex-rs/core/src/context_manager/history.rs40-60 codex-rs/core/src/context_manager/history.rs143-146 codex-rs/core/src/context_manager/normalize.rs20-130 codex-rs/core/src/state/session.rs26-28
The ContextManager serves as the bridge between high-level session state and the low-level ResponseItem storage used for API communication.
ContextManager Fields
| Field | Type | Purpose |
|---|---|---|
items | Arc<Vec<ResponseItem>> | Ordered history from oldest to newest; uses Arc for efficient snapshots codex-rs/core/src/context_manager/history.rs43 |
history_version | u64 | Incremented whenever history is rewritten (compaction/rollback) codex-rs/core/src/context_manager/history.rs45 |
token_info | Option<TokenUsageInfo> | Last API-reported token usage and context window state codex-rs/core/src/context_manager/history.rs46 |
world_state_baseline | Option<WorldStateSnapshot> | Most recent state appended to history for diffing codex-rs/core/src/context_manager/history.rs59 |
Sources: codex-rs/core/src/context_manager/history.rs40-60 codex-rs/core/src/context_manager/history.rs143-146
Codex uses a "World State" system to inject environment details (CWD, shell status, date) and agent instructions into the conversation history. The ContextManager handles the lifecycle of these snapshots.
WorldStateSnapshot of the current environment codex-rs/core/src/context_manager/history.rs95ContextManager::update_world_state compares the current snapshot against the world_state_baseline codex-rs/core/src/context_manager/history.rs97-98ContextualUserFragment items representing only what changed (e.g., a new CWD) codex-rs/core/src/context_manager/history.rs107WorldStateItem (Full or Patch) for persistence in the rollout file codex-rs/core/src/context_manager/history.rs98-105Sources: codex-rs/core/src/context_manager/history.rs91-108 codex-rs/core/src/context_manager/history.rs59-60
Items flow into the history manager through record_items, which accepts a TruncationPolicy codex-rs/core/src/context_manager/history.rs124-128
Before history is sent to the model via for_prompt, normalize_history ensures structural integrity codex-rs/core/src/context_manager/history.rs143-146:
ensure_call_outputs_present function in normalize.rs inserts synthetic "aborted" outputs for missing ones to satisfy model requirements codex-rs/core/src/context_manager/normalize.rs20-130remove_orphan_outputs strips tool outputs that do not have a corresponding call in the surviving history (often after compaction) codex-rs/core/src/context_manager/normalize.rs147-174input_modalities codex-rs/core/src/context_manager/history.rs141-143Tool outputs (like large file reads) are truncated based on the TruncationPolicy to prevent a single tool result from exhausting the entire context window codex-rs/core/src/context_manager/history.rs124-138
Sources: codex-rs/core/src/context_manager/history.rs124-146 codex-rs/core/src/context_manager/normalize.rs20-130 codex-rs/core/src/context_manager/normalize.rs147-174
Codex maintains context window usage tracking to trigger proactive management and warn the model.
TokenUsage, which is updated in the session state and stored in token_info codex-rs/core/src/state/session.rs138-144 codex-rs/core/src/context_manager/history.rs75-77estimate_token_count uses byte-based heuristics (via approx_token_count) to provide a lower bound codex-rs/core/src/context_manager/history.rs164-171TokenBudgetRemainingContext and TokenBudgetReminder are injected into the "developer" role to inform the model of remaining capacity codex-rs/core/src/context/token_budget_context.rs103-171Sources: codex-rs/core/src/context_manager/history.rs162-171 codex-rs/core/src/state/session.rs138-144 codex-rs/core/src/context/token_budget_context.rs103-171
History is not strictly additive. Approaches to context management include:
replace method allows the Session to overwrite the entire history vector, typically used after a successful compaction codex-rs/core/src/state/session.rs114-123reconstruct_history_from_rollout scans the rollout file (newest-to-oldest) to rebuild the ContextManager state, including world_state_baseline and previous_turn_settings codex-rs/core/src/session/rollout_reconstruction.rs113-117AutoCompactWindow to manage the transition between full context and summarized history codex-rs/core/src/state/session.rs158-188For details on the summarization logic and triggers, see History Compaction System.
Sources: codex-rs/core/src/state/session.rs114-123 codex-rs/core/src/session/rollout_reconstruction.rs113-117 codex-rs/core/src/state/session.rs158-188
Refresh this wiki
This wiki was recently refreshed. Please wait 4 days to refresh again.