Skip to content

[FEATURE]: Expose context budget information to agents #19471

Closed as not planned
Closed as not planned

Description

@SerhiiTopor

Feature hasn't been suggested before.

  • I have verified this feature I'm about to request hasn't been suggested before.

Describe the enhancement you want to request

Problem

When OpenCode spawns an agent via the Task tool, the agent receives environment context (model, OS, cwd) but has no visibility into its context budget. Specifically, the agent does not know:

  • The model's maximum context window size
  • How many tokens have been consumed so far
  • How many tokens remain before compaction triggers
  • The configured compaction threshold
  • Its step limit (until it hits MAX_STEPS, which is too late to plan around)

This means agents cannot make informed decisions about:

  • Whether to dive deep or stay shallow on research
  • When to proactively summarize/compact before forced compaction loses nuance
  • How to allocate remaining context across multiple subtasks
  • Whether to spawn further subagents vs. doing work inline

Current behavior

The system prompt constructed in session/prompt.ts → system.ts includes model name, platform, date, and project instructions. Token budgets are managed internally by compaction.ts (thresholds, pruning) but never surfaced to the agent.
The agent only learns about limits when it hits MAX_STEPS — a hard stop that forces a summary, rather than enabling graceful degradation.

Proposed solution:
Add a block to the system prompt (or as a system-reminder injected periodically) containing:

<context-budget>
  max_context_tokens: 200000
  used_tokens: 47823 -- overall including compactions
  remaining_context_tokens: 152177
  compaction_threshold: 0.9
  compaction_triggers_at: 180000
  compactions_total: 0
  current_step: 3
  max_steps: 25
</context-budget>

Implementation options (non-exhaustive):

  1. Static injection at session start — include max_context_tokens, compaction_threshold, and max_steps in the environment prompt built by SystemPrompt.environment() in session/system.ts. Low cost, gives agents planning info upfront.
  2. Dynamic system-reminder on each turn — inject current used_tokens and remaining_tokens as a system reminder in prompt.ts message assembly. Higher value but adds overhead per turn.
  3. New read-only tool context_budget — agents can query their budget on demand. Most flexible, zero overhead when not called. Could return live token counts from the session.

Option 1 is the minimum viable version. Option 3 is the most elegant long-term.
Why this matters
Agentic coding tasks are increasingly long-running and multi-step. Without budget awareness, agents either:

Waste context on verbose exploration then hit compaction mid-task (losing important reasoning)
Are overly conservative because they can't gauge remaining capacity
Spawn subagents unnecessarily (or fail to spawn them when they should)

This is especially impactful for the general and explore subagent types that handle open-ended research.
Related issues

#8140 — Configurable compaction threshold (framework-level, not agent-facing)
#13003 — Token usage display in TUI (user-facing, not agent-facing)
#18100 — Subagent infinite recursion (budget awareness would help agents self-limit)
#18037 — Large instruction files consuming context (agents could defer loading if budget-aware)

Activity

  1. added
    discussionUsed for feature requests, proposals, ideas, etc. Open discussion
    on Mar 27, 2026
  2. added
    coreAnything pertaining to core functionality of the application (opencode server stuff)
    on Mar 27, 2026
  3. github-actions commented on Mar 27, 2026

    @github-actions
    Contributor

    This issue might be a duplicate of existing issues. Please check:

  4. removed
    discussionUsed for feature requests, proposals, ideas, etc. Open discussion
    coreAnything pertaining to core functionality of the application (opencode server stuff)
    on May 3, 2026
  5. github-actions commented on Jul 5, 2026

    @github-actions
    Contributor

    To stay organized issues are automatically closed after 60 days of no activity. If the issue is still relevant please open a new one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions