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, search_code, grep, shell, others).
  2. The size distribution of tool results (median, 90th percentile, largest) and how many are cut by the 8,000-character cap.
  3. Whether old tool results can leave the history safely: test the open options first (rule 3), for example the history processors and context management features of pydantic_ai and pydantic_ai_harness, before any new code. State which options exist in the pinned versions and what each does.
  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 4,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 call budget and notice (#4346), whole-line cut (#4341), the start context inventory issue.

Dependencies

Alternatives check of 2026-11-20: not-applicable.
Class: chore, no search.
Tool: none found.

Proposed design

Problem statement

Do not start this item before #6521 closes.

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. In a stage plan run of #3387, tool results were about 55 percent of the context at the end. 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. It lists the open options that drop or compact old results with evidence from their source. It 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 a design.md that holds the report, the options and the recommendation.

Scenario: OpenSpec accepts the change folder

  • WHEN uv run devops openspec check runs on the branch.
  • THEN The command exits 0, and the change folder holds proposal.md, design.md and tasks.md.

Requirement: Context share by kind

The design note SHALL give the input token share by kind at the end of each 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. It gives the count cut at 8,000 characters in the stage run and at 40,000 characters in the implement run.

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 8,000-character cap of the stage planner or the 40,000-character cap of the implement step.

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 fix issue 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 that it files after the merge.

Requirement: Pending a person after the merge

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

Scenario: The issue holds the summary and the fix issues

Existing Tool Considered

The alternatives check in the issue body records not-applicable. The change is a measurement document and builds no code. It has no build cost. It reads the pinned pydantic_ai and pydantic_ai_harness compaction options as the subject of the analysis. No open tool reports the token share by kind or the tool result size distribution.

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, docs/TESTING.md, openspec/changes/archive/2026-11-07-3242-design-a-sandboxed-read-only-run/proposal.md, openspec/changes/archive/2026-11-07-3242-design-a-sandboxed-read-only-run/.openspec.yaml, openspec/changes/archive/2026-11-07-3242-design-a-sandboxed-read-only-run/tasks.md]
    • 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. Readiness requires a pull request to add or modify a file under its 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.59.0 provides ProcessHistory, which rewrites the message history before each request. The pinned pydantic_ai_harness 0.59.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 already uses the three harness capabilities. (best practice) [sources: pyproject.toml, src/devops_cli/roadmap/implement/implement_step.py, src/devops_cli/roadmap/stage/stage_agent.py, 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, CHANGELOG.md]
    • Chosen: pydantic_ai ProcessHistory and pydantic_ai_harness compaction. Both are open, maintained and pinned, and the harness compaction module is already imported and tuned by the step.
    • Rejected: Own compaction in devops_cli.ai.harness. The project deleted it in refactor(ai): delete the unwired harness port and the capability clones nothing imports #1836 because it copied what the libraries ship.
    • 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 implement trace keeps each model call's tokens_in and tokens_out and each tool call's name and outcome. It holds no argument and no output. The stage RunTrace keeps each tool call's answer size in characters and the last request's input tokens. The per-request growth is read from these records. The size distribution and the share by kind are measured on the run from the stage character counts and the implement message history. The note names the source of each figure. [sources: src/devops_cli/roadmap/implement/implement_trace.py, src/devops_cli/roadmap/agent_run.py, src/devops_cli/roadmap/stage/stage_plan_record.py, docs/ROADMAP_WORKFLOW.md, openspec/changes/archive/2026-11-10-4341-a-tool-answer-cut-at-the/proposal.md]
  • 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 8,000-character cap of the stage planner and the 40,000-character cap of the implement step. The note reports the count that each cap cuts. A 4,000-character cap or a compaction is a recommendation for a follow-up issue. (best practice) [sources: src/devops_cli/config/constants.py, src/devops_cli/roadmap/stage/stage_tools.py, src/devops_cli/roadmap/implement/implement_step.py, openspec/changes/archive/2026-11-10-4341-a-tool-answer-cut-at-the/proposal.md]
    • Chosen: Keep both caps and measure. The item has no evidence yet for a smaller cap, the stage cap already has its notice from fix(roadmap): a tool answer cut at the character limit has no notice #4341, and the body forbids a code change in this item.
    • Rejected: Set the stage cap to 4,000 characters 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 top tools of each job plus one other group. The stage planner groups read_file, search_code, list_files, git_show, references and other. The implement step groups read_file, grep, shell, list_files, edit_file, write_file and other. (best practice) [sources: docs/roadmap/stage.md, src/devops_cli/roadmap/stage/stage_tools.py, src/devops_cli/roadmap/implement/implement_step.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 search_code only in the stage planner.
    • 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 summary 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: openspec/changes/archive/2026-11-07-3242-design-a-sandboxed-read-only-run/proposal.md, openspec/changes/archive/2026-11-07-3242-design-a-sandboxed-read-only-run/tasks.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

#6521

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.

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/newtype/chorevalue/medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions