Skip to content

chore(roadmap): measure how context grows during a step #4386

Open

Description

@dan-petty

Problem

The context of a step grows with every tool call, and each request sends the whole context again. In the first attempt of group 1 of #4004 the input per request grew from 97,821 to 140,903 tokens in about 45 requests. In a stage plan run of #3387, tool results were about 55% of the context at the end (about 105,000 of 190,000 tokens). Only 3% of the lines that the planner read were read twice, so a guard against repeated reads saves almost nothing. The cost of the growth is high: the whole item used 22.6M input tokens in 211 requests.

What to find out

  1. The share of the context by kind at the end of a run, for implement and for stage: system and task text, tool definitions, model replies, tool results by tool (read_file, grep, list_files, the shell tool, others).
  2. The size distribution of tool results (median, 90th percentile, largest) and how many the cap of the tool results cuts (ToolOutputLimits, with CONST_ROADMAP_IMPLEMENT_TOOL_MAX_CHARS in implement).
  3. Whether old tool results can leave the history safely: test the open options first (rule 3), for example the history processors of pydantic_ai and the context controls of pydantic_ai_harness, before any new code. Coder already gives ClearToolResults and WarnNearLimits, and implement (implement_step.py) and stage (stage_agent.py) set their window to the window of the task. State what these controls do in the pinned versions and which other options exist.
  4. For each option: the effect on the number of tokens, the risk that the agent loses a fact it needs (re-reads, repeated mistakes, failed checks), and a way to measure that risk offline (the golden stage eval, the implement eval).
  5. Whether a smaller cut for large tool results (for example a value below CONST_ROADMAP_IMPLEMENT_TOOL_MAX_CHARS, which is 40,000 characters, with a clear notice) is better than a compaction of old results.

Done when

A table of the context share, a list of the open options with evidence from their source or documents, and a ranked recommendation are in this issue. No code change is part of this issue; file a fix issue for each recommendation.

Related: the start context inventory (#4385).

Alternatives check of 2026-11-30: not-applicable.
Class: chore, no model call.
Tool: none found.

Proposed design

Problem statement

The context of a step grows with every tool call, and each request sends the whole context again. In one group 1 run of #4004 the input per request rose from 97,821 to 140,903 tokens in about 45 requests, and in a stage plan run of #3387 tool results were about 55 percent of the context at the end. Only 3 percent of the lines that the planner read were read twice, so a guard against repeated reads saves almost nothing. The whole item used 22.6 million input tokens in 211 requests. This item measures the context share by kind and the tool result sizes for one implement run and one stage plan run, lists the open options that drop or compact old results with evidence from their source, and records a ranked recommendation with an offline risk check. It changes no code.

Acceptance criteria

Requirement: Documentation-only change folder

The change SHALL add the OpenSpec change folder openspec/changes/4386-measure-how-context-grows-during-a-step/ with proposal.md, design.md, tasks.md and .openspec.yaml.

Scenario: OpenSpec accepts the change folder

  • WHEN uv run devops openspec check runs on the branch.
  • THEN The command exits 0, and the folder holds the four files and .openspec.yaml sets skip_specs: true.

Requirement: Context share by kind

The design note SHALL give the input token share by kind at the end of one implement run and one stage plan run.

Scenario: The shares add to 100 percent

  • WHEN A reviewer reads the design note against the recorded run traces.
  • THEN The note gives one share for system and task text, tool definitions, model replies, and tool results by tool, and the shares add to 100 percent for each run.

Requirement: Tool result size distribution

The design note SHALL give the median, 90th percentile and largest tool result size, and the count each cap cuts, for each run.

Scenario: Known result sizes

  • WHEN A reviewer reads the size table and the recorded tool calls of the two runs.
  • THEN The table gives the median, the 90th percentile and the largest size, the count cut at the implement cap of 40,000 characters and at each other cap that a job sets, and each cap value with its source.

Requirement: Options with a source

The design note SHALL list each open option with what it does in the pinned versions and a source.

Scenario: Each option names a source

  • WHEN uv run devops openspec check runs and a reviewer reads the options section.
  • THEN The command exits 0, and each row names a repository path or a URL and the pinned version from pyproject.toml.

Requirement: Ranked recommendation with a risk check

The design note SHALL rank the options and give each its token effect, its risk to needed facts, and an offline risk check.

Scenario: Ranked options with a risk check

  • WHEN A reviewer reads the recommendation of the design note.
  • THEN The options are ranked, and each names the golden stage eval or the implement eval as its risk check.

Requirement: No code and unchanged caps

The change MUST add documents only and MUST NOT change the 40,000-character cap of the implement step or any cap of the stage planner.

Scenario: The diff holds no source change

  • WHEN git diff --name-only origin/main...HEAD and uv run devops openspec check run.
  • THEN The diff lists no path under src/, the command exits 0, and the change folder .openspec.yaml sets skip_specs: true.

Requirement: One fix issue per recommendation

The change's tasks.md SHALL list one follow-up task for each recommendation.

Scenario: A recommendation without a follow-up

  • WHEN A reviewer reads tasks.md and the ranked recommendation.
  • THEN One task exists for each recommendation, and each task names the recommendation and the fix issue title that it files after the merge.

Requirement: Pending a person after the merge

After the change merges, a person SHALL post the report in issue #4386 and file one fix issue for each recommendation.

Scenario: The issue holds the report and the fix issues

Existing Tool Considered

The alternatives check in the issue body records not-applicable and Tool: none found. The analysis assesses the open, maintained pydantic_ai 2.77.0 and pydantic_ai_harness 0.77.0 as the options to drop or compact old results. Neither reports the token share by kind or the tool result size distribution. The change builds no measurement code, so it pays no build cost.

Key questions

  • How does one pull request deliver an item whose result is an analysis?: The pull request adds a documentation-only OpenSpec change folder. Its design.md holds the measured table, the options with sources and the ranked recommendation. .openspec.yaml sets skip_specs: true. The repeatable check is uv run devops openspec check. (best practice) [sources: docs/adr/0010-openspec-is-the-spec-and-change-proposal-layer.md, docs/ROADMAP_WORKFLOW.md, openspec/config.yaml]
    • Chosen: Documentation-only change folder with a design note. ADR 0010 makes every roadmap item one change folder, and a document-only change sets skip_specs: true.
    • Rejected: Analysis in the issue only. The roadmap workflow requires a change folder, so an issue comment leaves no deliverable to check.
    • Rejected: A committed report command and its tests. The body says no code change is part of this issue, and a document gets no test.
  • Which library drops or compacts old tool results?: The pinned pydantic_ai 2.77.0 provides history processors that rewrite the message history before each request. The pinned pydantic_ai_harness 0.77.0 provides ClearToolResults, WarnNearLimits and ToolOutputLimits. ClearToolResults clears old tool results against the context window. WarnNearLimits warns near that window. ToolOutputLimits cuts a result at a character cap. The implement step uses the output cap with CONST_ROADMAP_IMPLEMENT_TOOL_MAX_CHARS. The implement step and the stage agent use ClearToolResults and set the window to the task window. (best practice) [sources: pyproject.toml, src/devops_cli/roadmap/implement/implement_step.py, src/devops_cli/roadmap/stage/stage_agent.py, src/devops_cli/config/constants.py:4039, src/devops_cli/ai/knowledge_base/devops_cli/libraries/pydantic_ai_harness.md, src/devops_cli/ai/knowledge_base/devops_cli/libraries/pydantic_ai.md]
    • Chosen: pydantic_ai history processors and pydantic_ai_harness compaction. Both are open, maintained and pinned. The harness compaction module is already imported and tuned by the step.
    • Rejected: Own compaction in devops_cli.ai.harness. The body asks to test the open options before any new code, and an own layer adds code to keep and test.
    • Rejected: No compaction, only a smaller character cap. It cuts each result but keeps every result in the history, so the context still grows with each call.
  • Which recorded data does the measurement read?: The measurement reads the recorded run traces and the eval records. It names the source of each figure. When a trace does not hold a figure, the note says so and names the source that does. It does not add a code change. [sources: src/devops_cli/roadmap/agent_run.py:424, src/devops_cli/roadmap/implement/implement_step.py, src/devops_cli/roadmap/stage/stage_agent.py, src/devops_cli/roadmap/implement/implement_eval.py, src/devops_cli/roadmap/stage/stage_eval_gates.py]
  • How is the risk of an option measured offline?: The golden stage replay judges the recorded plans with no model and no git. The implement eval checks the four diff properties on recorded results. Each recommendation names one of these offline checks. It states that the live devops roadmap eval stage run is the check for the rest. (best practice) [sources: src/devops_cli/commands/roadmap_eval_stage.py, src/devops_cli/roadmap/stage/stage_eval_gates.py, src/devops_cli/roadmap/implement/implement_eval.py, tests/roadmap/stage/test_roadmap_stage_eval_gates.py, tests/roadmap/implement/test_roadmap_implement_eval_properties.py]
    • Chosen: Golden stage replay and implement eval recorded results. Both run offline on recorded plans and results, so a person can run them again with no model call.
    • Rejected: The live devops roadmap eval stage run only. It is a model run, so it is not an offline check and it costs the tokens this item measures.
    • Rejected: No risk check. The item asks for the risk of each option and a way to measure it offline.
  • Does the change set a smaller character cap for large tool results?: No. The change keeps the 40,000-character cap of the implement step and any cap of the stage planner. The note reports the count that each cap cuts. A smaller cap or a compaction is a recommendation for a follow-up issue. (best practice) [sources: src/devops_cli/config/constants.py:4039, src/devops_cli/roadmap/implement/implement_step.py, src/devops_cli/roadmap/stage/stage_agent.py]
    • Chosen: Keep both caps and measure. The item has no evidence yet for a smaller cap, and the body forbids a code change in this item.
    • Rejected: Set a smaller cap now. It changes the planner's input before the measurement exists, and it needs a code change that the body leaves to a fix issue.
    • Rejected: Remove the caps. A large result would raise the cost of every later request, which is the growth this item measures.
  • Which groups does the report divide the tool results into?: The report uses the tool names in the recorded calls of each job, with one other group. It does not fix a list before it reads the traces. (best practice) [sources: src/devops_cli/roadmap/implement/implement_step.py, src/devops_cli/roadmap/stage/stage_agent.py]
    • Chosen: Top tools of each job plus other. The two jobs run different tools, so one fixed list would hide the tools that dominate each job.
    • Rejected: One fixed list for both jobs. grep and the shell exist only in the implement step, and the stage planner has its own tools.
    • Rejected: One group for all tool results. It would not show which tool causes the growth, which is the question of the item.
  • How are the fix issues for the recommendations delivered?: The change's tasks.md lists one pending person task for each recommendation. After the merge, a person posts the report in issue chore(roadmap): measure how context grows during a step #4386 and files one new issue for each recommendation. Each issue names this issue and the recommendation. (best practice) [sources: docs/ROADMAP_WORKFLOW.md, docs/adr/0010-openspec-is-the-spec-and-change-proposal-layer.md]
    • Chosen: One issue per recommendation. Each recommendation is built and closed on its own, and the body asks for a fix issue for each one.
    • Rejected: One issue for all recommendations. A single issue cannot be closed when one recommendation ships and another waits, and it hides the ranking.
    • Rejected: No follow-up issue. The body says a fix issue follows each recommendation, so the analysis would change nothing.

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Activity

  1. added
    source/agent
    type/chore
    scope/roadmap
    status/new
    value/medium
    effort/medium
    on Nov 10, 2026
  2. dan-petty commented on Nov 10, 2026

    @dan-petty
    OwnerAuthor

    Intake placed this item to the backlog: intake puts every new item in the backlog. A person plans the milestones.

    • Type: type/chore. The chore prefix of the title gives the type.
    • Scope: scope/roadmap. The (roadmap) scope of the title gives the scope.
    • Priority: P2-Medium. It produces the data needed to choose a context-management fix, but it is research, not a user-facing change.
    • Value: Medium. It delivers a ranked recommendation that guides the next fix issues for context growth.
    • Effort: Medium. It requires running experiments, collecting token distributions, testing pydantic_ai options, and writing a table, but no code change.
    • Related: chore(roadmap): inventory the start context of a step #4385, which asks for a different change. Both measure step context, but #4385 inventories the first request while this issue measures growth and end-of-run share by kind.
  3. added
    priority/p2-medium
    on Nov 10, 2026
  4. added
    status/ready
    and removed
    status/new
    on Nov 10, 2026
  5. devops-cli-service commented on Nov 10, 2026

    @devops-cli-service
    Contributor

    Status set to Ready at f29d202 (see proposed design in issue body).

  6. added
    owner/agent
    on Nov 11, 2026
  7. devops-cli-service commented on Nov 11, 2026

    @devops-cli-service
    Contributor

    Alternatives check: not-applicable

    Class: chore. The check makes no search and no model call for an item of this class.

    Tool: none found

    Checked on 2026-11-11. A person decides what this means for the item. The check changes no label and closes no issue.

  8. dan-petty commented on Nov 13, 2026

    @dan-petty
    OwnerAuthor

    Two experiments for the stage planner that use this study, from the start-context review of the roadmap jobs (2026-11-13).

    Evidence (stage comments of 54 items, 2026-11-06 to 2026-11-13, 8,213 calls):

    • read_file: 3,290 calls for 991 distinct item and path pairs (70% repeats).
    • Exact duplicates of tool and arguments: 1,558 of 8,213 (19%). The trace joins all runs of an item, so the share inside one run is not known.
    • A refused reply starts a new run with only the request, previous and problem (stage.py _Refused.returned). The new run has no tool results and starts at call 1.
    • Loki, 7 days: 263 stage reply refused lines over 47 items (max 20, for ci(image): move the image checks to one path-filtered workflow #1858). Item attempts reach 14-17M tokens.

    Experiment A: a gate return continues the plan run (pydantic-ai message_history).

    • Case set: the stage eval cases in live mode, paired by case, that get at least one gate return.
    • Arm 1: today.
    • Arm 2: continue only after the first one or two gate returns. Trim once, in one large step near a limit, never on each request. A trim changes earlier messages, so the cache misses for all text after it.
    • Arm 3: a new run that gets the paths and line ranges that the earlier runs read, with no text. _trace_facts already joins the plan runs, so the gate accepts these reads.
    • Primary measure: model requests of each retry run. Also: the pass rate after returns, returns for each item, tokens and seconds for each item, timeouts, the largest request and the cache-read share. The first-reply score cannot change, so it is no measure here.
    • Decision rule: an arm passes if a retry uses at most 60% of the requests of a new run, and the pass rate after returns stays inside a margin set before the run.
    • Notes: a continued run starts at about 91k and reaches about 190k after two returns (window 256k), so arm 2 stops after 2 returns. Record only the new calls of a continued run, or RunTrace.combined counts them twice. A trim that clears tool answers lets the gate accept claims about text that the model no longer has. Needs the run index of feat(roadmap): run records give the run, step and path class of tool calls #5460. A review read manifest is a separate arm, only after fix(roadmap): a review finding must name a part of the plan to block it #5410 and fix(roadmap): only blocker and major review findings block a plan #5411 merge.

    Experiment B: a pointer answer for a repeat call inside one run (stage toolbox only; it reads one fixed commit).

    • Waits for feat(roadmap): run records give the run, step and path class of tool calls #5460. File it only if the duplicate share inside one run is material and a pointer lowers the requests for each run.
    • A pointer saves only the answer tokens (about 1.1k each). The call and the turn stay, and output is most of the stage cost.
    • Otherwise close it as covered by experiment A.
    • Not for implement: git status repeats follow edits, and ClearToolResults removes old answers.
  9. dan-petty commented on Nov 20, 2026

    @dan-petty
    OwnerAuthor

    Frozen on 2026-11-20. The stage, implement and fix-checks jobs grade under 60%, so the owner rule asks for a re-architecture before more fixes inside the current design (tracker #6519). This item is incremental work on those jobs, so its Dependencies section now names #6521 first. Stage and implement leave this item while #6521 is open. When the experiments of #6519 decide, a person closes #6521 (this item can then continue) or closes this item as not planned.

  10. added
    status/new
    and removed
    status/ready
    on Nov 26, 2026
  11. devops-cli-service commented on Nov 26, 2026

    @devops-cli-service
    Contributor

    Next step: the open items close, or a person replies in a comment or edits the body. See Dependencies in the section Proposed design of the body.

  12. added
    status/ready
    and removed
    status/new
    on Nov 30, 2026
  13. devops-cli-service commented on Nov 30, 2026

    @devops-cli-service
    Contributor

    Next step: the stage job writes the plan of the item. See the section Proposed design of the body.

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

    effort/mediumowner/agentpriority/p2-mediumscope/roadmapsource/agentstatus/readytype/chorevalue/medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions