Skip to content

[FEATURE]: Expose the remaining context budget to the agent #59531

Closed as not planned

Description

@aanbrn

Problem

In the OpenCode 2.1.4 session below, the session's stored system messages contain no remaining-budget, usage, or token
figure, and no assistant record carries one either. An agent therefore cannot tell "near the limit" from "well inside
the window". That session concluded its budget was exhausted while its per-request context measured about half the
model's window, and then changed how it worked — one step per reply instead of continuing the task.

Measured case

Environment: OpenCode 2.1.4 (opencode --version; Homebrew formula opencode-v2), macOS; model
opencode-go/deepseek-v4.1-flash with limit.context 1,000,000 and limit.output 384,000, from the provider catalog
the CLI ships.

A long build session (289 assistant steps) ended two of its turns with assistant prose stating that its session budget
was exhausted — at 09:32:16 and 09:33:34 local, translated: "the budget of this session is exhausted" — and continued one
work step per user reply instead of stopping. Over the preceding interval the owner sent seven replies, five of them a
bare "continue" (translated). Per-request usage recorded for those turns: input 56–2,594 tokens plus cache.read
527,360–540,032 — roughly 530–540k cached input tokens, about 54 % of the 1,000,000-token window.

The session's record:

  • instructions received: the session stores 13 system messages — instruction notices for the Code Mode catalog, the
    date, available skills, and AGENTS.md edits — and none carries a remaining-budget, usage, or token-count field;
  • no step limit: opencode debug agents reports steps unset for all 18 agents;
  • no compaction: the session's time_compacting is null;
  • no truncation: turn finishes are 272 tool-calls and 17 stop, none length, and no turn carries an error;
  • no provider quota response: no 429 or usage-limit body appears in the session, and its recorded cost is $0.605.

The owner then invoked a pause command, which persisted the session state, and a new session continued the work.

Proposed solution

Expose the session's remaining context budget to the agent — in the system prompt, a system reminder, or a read-only tool
— or state explicitly that no measurement is available. Either lets an agent distinguish "near the limit" from "well
inside the window" instead of inferring a limit. A small tool schema costs a fixed handful of tokens on every request;
the reading itself costs nothing until it is called, and an explicit "unavailable" is the minimum viable form.

What this case does not establish

  • whether the missing visibility caused the assessment — this is one session on one provider;
  • whether the statement referred to the context window rather than some other budget the model inferred; the record rules
    out a step limit, compaction, truncation, and a provider quota, not every conceivable budget;
  • whether an account or provider limit outside the recorded session was involved; the record shows none;
  • whether other models or providers behave the same way.

Related

Activity

  1. opencode-agent commented on Oct 27, 2026

    @opencode-agent
    Contributor

    Thanks for the detailed write-up and the measurements, @aanbrn.

    You're asking for the agent to be told how much of its context window remains, or explicitly told that no figure is available. That way it can tell "near the limit" from "well inside the window" instead of guessing at a limit, as happened at about 54% of the window in your session. Right now OpenCode v2 doesn't give the agent this information, so I'm treating this as a feature request. Maintainers will consider it.

    One design point to raise: a figure that changes every turn should go somewhere that doesn't break prompt caching, such as a read-only tool or a reminder on the latest message, rather than in the system prompt.

    Related issues:

  2. rekram1-node commented on Oct 27, 2026

    @rekram1-node
    Contributor

    weve seen even newer sota models get confused by this, for now if you want it you should write a plugin for it. We may later add if we feel it significantly increases performance and has little downsides

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

Metadata

Metadata

Assignees

No one assigned

    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