Support default subagent context window and auto-compaction limits
What variant of Codex are you using?
Codex Desktop / CLI with native multi-agent / subagent support.
What feature would you like to see?
Please add global default context-window and auto-compaction settings specifically for spawned subagents, analogous to the existing subagent defaults for model and reasoning effort.
For example:
[agents]
default_subagent_model = "gpt-6-sol"
default_subagent_reasoning_effort = "high"
default_subagent_context_window = 272000
default_subagent_auto_compact_token_limit = 244800
The parent agent should continue using the normal top-level settings:
model = "gpt-6-astra"
model_reasoning_effort = "medium"
model_context_window = 1000000
model_auto_compact_token_limit = 800000
Motivation
There is a common mixed-model orchestration case where the parent and all spawned workers should use different context policies:
- A long-lived GPT-6 Astra parent acts as the orchestrator and may intentionally use a large context window.
- Spawned GPT-6 Sol workers handle bounded implementation, debugging, testing, or review tasks.
- Those Sol workers should compact earlier and remain below a smaller context boundary without forcing the parent to use the same low auto-compaction limit.
Conceptually:
GPT-6 Astra parent
1M context
800K auto-compact
|
+-- GPT-6 Sol worker
| 272K max / 244.8K auto-compact
|
+-- GPT-6 Sol worker
| 272K max / 244.8K auto-compact
|
+-- GPT-6 Sol worker
272K max / 244.8K auto-compact
Today, context-window and auto-compaction settings are effectively parent/global settings for spawned agents. This creates an awkward tradeoff:
- Keep the global auto-compaction threshold low, which prevents a large-context parent from making full use of its context window; or
- Raise the global threshold for the parent, which also allows spawned workers to accumulate much more context than desired.
Why a subagent default is useful
This request is intentionally narrower than full per-agent context configuration.
Codex already supports global defaults such as:
[agents]
default_subagent_model = "gpt-6-sol"
default_subagent_reasoning_effort = "high"
Adding:
default_subagent_context_window = 272000
default_subagent_auto_compact_token_limit = 244800
would solve the common parent/worker case with a simple configuration model, while preserving current behavior when these settings are absent.
Suggested precedence
A possible precedence model:
explicit agent-role override
>
[agents] default_subagent_* setting
>
parent/top-level setting
>
model/runtime default
This would also compose naturally with future or existing role-specific overrides.
Related issues
- #40377 requests separate context limits for subagents.
- #45164 requests per-agent context-window overrides in agent roles.
This request is narrower than #45164: it asks for one global default context policy for all spawned child/subagents, analogous to default_subagent_model and default_subagent_reasoning_effort, without requiring every role to define its own context settings.
I traced the current main implementation and put together a minimal implementation plan for this request.
The good news is that this should not require changes to the V1/V2 spawn handlers separately, nor to the agent-role override system.
Current path
Both multi-agent backends eventually go through:
[agents]
default_subagent_model
default_subagent_reasoning_effort
↓
AgentsToml
↓
Config.agent_default_subagent_*
↓
prepare_agent_spawn_config()
↓
apply_requested_spawn_agent_model_overrides()
↓
AgentControl.spawn()
Both V1 and V2 call prepare_agent_spawn_config() in codex-rs/core/src/agent/child_config.rs, so the child context override can be implemented once there.
Minimal implementation
1. Extend AgentsToml
In codex-rs/config/src/config_toml.rs:
pub struct AgentsToml {
// existing
pub default_subagent_model: Option<String>,
pub default_subagent_reasoning_effort: Option<ReasoningEffort>,
// proposed
pub default_subagent_context_window: Option<i64>,
pub default_subagent_auto_compact_token_limit: Option<i64>,
}
This would support:
[agents]
default_subagent_model = "gpt-6-sol"
default_subagent_reasoning_effort = "high"
default_subagent_context_window = 272000
default_subagent_auto_compact_token_limit = 244800
2. Carry the settings into runtime Config
In codex-rs/core/src/config/mod.rs, add:
pub agent_default_subagent_context_window: Option<i64>,
pub agent_default_subagent_auto_compact_token_limit: Option<i64>,
and resolve them from cfg.agents alongside the existing model/reasoning defaults.
3. Apply the defaults when deriving a child config
The key location is prepare_agent_spawn_config() in:
codex-rs/core/src/agent/child_config.rs
A small helper could be added:
fn apply_configured_subagent_context_defaults(
config: &mut Config,
context_window: Option<i64>,
auto_compact_token_limit: Option<i64>,
) {
if let Some(context_window) = context_window {
config.model_context_window = Some(context_window);
}
if let Some(auto_compact_token_limit) = auto_compact_token_limit {
config.model_auto_compact_token_limit = Some(auto_compact_token_limit);
}
}
and called after the parent-derived child config is built, but before model metadata is resolved:
let mut config =
build_agent_spawn_config(&session.get_base_instructions().await, step_context)?;
apply_configured_subagent_context_defaults(
&mut config,
turn.config.agent_default_subagent_context_window,
turn.config.agent_default_subagent_auto_compact_token_limit,
);
apply_requested_spawn_agent_model_overrides(
session,
step_context,
&mut config,
options.model,
options.reasoning_effort,
)
.await?;
The ordering matters because apply_requested_spawn_agent_model_overrides() resolves the child model with:
config.to_models_manager_config()
which already carries:
model_context_window
model_auto_compact_token_limit
So the child-specific context settings should be present before the final child model metadata is resolved.
Existing clamping can be reused
No new context-window validation logic should be necessary.
The models-manager already clamps configured context windows against a model's max_context_window:
if let Some(context_window) = config.model_context_window {
model.context_window = Some(
model
.max_context_window
.map_or(context_window, |max_context_window| {
context_window.min(max_context_window)
}),
);
}
Auto-compaction can likewise continue using the existing context-management path.
Suggested precedence
This would preserve current behavior and compose naturally with future role-specific support:
explicit agent-role context override
>
[agents] default_subagent_* context settings
>
parent/top-level model_context_* settings
>
model/runtime defaults
For this issue, only the middle layer needs to be added.
There is no need to modify AgentRoleOverrides for the minimal implementation; that remains the scope of #45164.
V1 / V2
No separate implementation should be necessary in:
codex-rs/core/src/tools/handlers/multi_agents/spawn.rs
codex-rs/core/src/tools/handlers/multi_agents_v2/spawn.rs
Both already converge on prepare_agent_spawn_config().
Tests
The existing subagent-default tests in:
codex-rs/core/tests/suite/subagent_notifications.rs
already cover:
spawn_agent_uses_configured_subagent_defaults
spawn_agent_uses_independent_configured_subagent_defaults
spawn_agent_preserves_configured_defaults_through_unrelated_role
The new behavior should cover at least:
- Both child context defaults override parent values.
- Neither child context default is set -> current parent inheritance remains unchanged.
- Context-window override only.
- Auto-compact override only.
- Model max-context clamping still works.
- V1 and V2 spawned children both receive the same child defaults.
A lightweight unit test around the helper would also avoid expanding ThreadConfigSnapshot just to expose these settings.
Full-history fork edge case
One boundary worth testing explicitly:
parent active history: > child configured context window
fork_context = true
For example, a parent may already hold 500K of history while the child default is 272K.
I would not make full-history forks silently ignore the child defaults, because that would make the same child policy behave differently depending on fork mode. The existing context-limit / compaction path should remain authoritative, with a dedicated test to confirm the resulting behavior.
Example end state
# Long-lived primary orchestrator
model = "gpt-6-astra"
model_reasoning_effort = "medium"
model_context_window = 1000000
model_auto_compact_token_limit = 800000
[agents]
default_subagent_model = "gpt-6-sol"
default_subagent_reasoning_effort = "high"
default_subagent_context_window = 272000
default_subagent_auto_compact_token_limit = 244800
This gives a large-context parent while keeping all spawned implementation workers bounded by a smaller default context policy, without requiring per-role context configuration.
+1, with one extension and some evidence from Codex CLI 0.159.2 (app-server, multi-agent v2).
Per-role override as well as a default. We spawn typed roles with different jobs: implementers and reviewers that should compact early, around 300K, and read-only explorers that benefit from a larger window, around 500K. A global default_subagent_* default would cover the common case. Honouring model_context_window / model_auto_compact_token_limit in a role file, or under [agents.<name>], would cover the rest, and would mirror how role files can already set model and model_reasoning_effort.
What happens today (0.159.2):
spawn_agentchildren inherit the parent'smodel_context_windowandmodel_auto_compact_token_limit.- Both keys are silently ignored in a role file and under
[agents.<name>], with no warning, while the same role's model override does apply. That makes the setting look supported when it is not. - A parent cannot steer a child's window either:
thread/compact/start,turn/startandsettings/updateaddressed to a child thread are refused with -32600.
If adding the keys is not planned soon, a warning when a role file sets a context key that will be ignored would save the next person a probe.
One source-level update for Codex 0.169.0: build_agent_shared_config clears inherited model_context_window and model_auto_compact_token_limit when SubagentDefaultContextLimits is enabled. This permits model defaults instead of parent inheritance, but still does not provide configurable numeric child limits.
The proposed settings would therefore remain useful even with that feature enabled. For example, a 128,000-token child window with compaction at 100,000 should affect newly spawned workers while preserving the parent's context policy. The resolved child model's supported maximum and required headroom should remain authoritative.
Useful acceptance cases would cover the feature both enabled and disabled, either setting supplied independently, and a full-history fork whose inherited history already exceeds the child limit. That last case should compact or return a clear error before an oversized model request.
Related: #45164 for role-specific overrides and #40429 for changing limits within an existing session. This is a check of the versioned source, not a fresh runtime reproduction.