Skip to content

fix(roadmap): the step budget counts new tokens, not cached input - #4553

Merged
dan-petty merged 2 commits into
mainfrom
fix/4387-step-budget-new-tokens
Nov 11, 2026
Merged

dan-petty merged 2 commits into
mainfrom
fix/4387-step-budget-new-tokens

Conversation

@dan-petty

@dan-petty dan-petty commented Nov 11, 2026 •

Copy link
Copy Markdown
Owner

The token budgets of roadmap.implement added the input_tokens of each reply, and pydantic-ai counts the cache reads in them. 98.6% of the runner input was cache reads, so steps stopped after about 100 requests.

  • The step budget, its ceiling and the item budget count new tokens: uncached input and output.
  • The defaults stay 8000000 and 20000000. A route with no cache reads, such as the local default ladder, counts each input token and keeps its room. On the cached route, the step time limit stops a step first.
  • The step outcome, the tier spend and implement.spend keep cache_read_tokens. The record keeps the item budget count as new_tokens. An older record has none and counts 0.
  • Stop reasons and extension lines say "new tokens".

This fix replaces the report-only criteria of the item. #4583 holds its open stage planner points 2 and 5.

Closes #4387

Change: openspec/changes/archive/2026-11-11-4387-the-step-budget-counts-new-tokens/

@dan-petty dan-petty added type/bug Type bug: the code does not do what it must scope/roadmap The roadmap jobs: intake, refine, stage, implement and the Service that runs them labels Nov 11, 2026
@dan-petty

Copy link
Copy Markdown
Owner Author

Live steps (a person)

The live ConfigMap devops/devops-cli-config sets roadmap.implement.step_token_budget: 12000000. Do these steps before the runner and Service image with this change rolls out:

  1. Delete the line step_token_budget: 12000000 under roadmap.implement. Do not set a new value: the new default is the value to use after the release (step_token_budget 1000000 new tokens, item_token_budget 2500000, step_token_ceiling 2000000).
  2. Why before: with this change alone, 12000000 is above the new default ceiling of 2000000, so the settings do not load and the Service and the runner Jobs do not start. fix(roadmap): default the step ceiling to twice the budget #4505 (PR fix(roadmap): default the step ceiling to twice the budget #4556) removes that failure, but 12000000 new tokens is still 12 times the new default. Until the new image rolls out, the image that runs now uses its own default of 8000000 input tokens.
  3. If you want a value in the ConfigMap after the release, set step_token_budget: 1000000 (the default), or a higher number of new tokens.

fail-to-pass

uv run devops ci fail-to-pass --base origin/main (25 min 44 s): 11 tests fail on the base and pass on this branch, and tests/roadmap/implement/test_roadmap_implement_limits.py does not collect on the base (no new_tokens). 108 changed tests pass on both.

  • test_the_input_that_the_provider_read_from_its_cache_does_not_count_for_the_step_budget, test_a_budget_counts_the_input_that_the_cache_did_not_serve_and_the_output: the new count.
  • test_spend_survives_kill_the_item_budget_counts_no_cached_input_of_an_earlier_job: the base stops the continuation with limit_reached on 100100 recorded input tokens, of which 100000 are cache reads.
  • 10 tests whose budgets now include the output tokens of the scripted replies (the step-level extension tests, the job extension comments, the item budget sums), and the defaults.

Other checks: pytest tests/roadmap/implement tests/roadmap/test_roadmap_limit_classes.py tests/security/test_config_declarations.py tests/config tests/ai/client/test_roadmap_agent_run.py: 1988 passed. uv run devops docs generate --sync-readme leaves no diff. The pre-push hook passed.

Measurements (2026-11-11)

  • Gateway metrics for end_user="devops-cli-job", one day at a time from 2026-11-05 to 2026-11-11: 950 to 1570 uncached input tokens for each request. 7 days: 2952952 output tokens in 8228 requests (about 360 for each request). The first day of the window had 171.7 million uncached input tokens in 3698 requests (46000 for each request), before the cache served the route.
  • The CLI reads the cached tokens through the gateway: devops_cli_ai_spend_usd_total of the Service row refine over 3 hours was USD 0.05228 for 1369114 prompt and 117587 completion tokens. At the rates of fix(ai): price the DeepSeek group and its cached input #4454 that needs about 1090000 cached prompt tokens (79%). With none, the cost would be USD 0.135.
  • The 4 stops of 2026-11-10 in the item comment had about 91000 to 154000 new tokens each.

The five points of the item

  1. The provider caches the shared prefix, and the CLI gets cache_read_tokens through the gateway (above, and pydantic-ai 2.59.0 maps prompt_tokens_details.cached_tokens).
  2. Implement step: 98.6% cache reads over 48 hours (item comment). Stage plan run: not measured in this change. The Service row refine: about 79%.
  3. Unit and limit: this pull request. Effect on the stops of 2026-11-10: each of the 4 limit_reached stops was at 9% to 15% of the new step budget, so none would stop on the token budget. The output_cap class does not change.
  4. Price: fix(ai): price the DeepSeek group and its cached input #4454 (merged).
  5. Prompt order: the 98.6% to 99.5% cache share of the implement Jobs shows a stable prefix. The order of the stage planner prompt is not checked in this change.

@dan-petty

Copy link
Copy Markdown
Owner Author

Review findings

  1. major, openspec/changes/archive/2026-11-11-4387-the-step-budget-counts-new-tokens/proposal.md: the pull request closes chore(roadmap): count cached tokens in the step budget #4387 but does not meet its acceptance criteria, and no follow-up issue holds points 2 and 5. Confirmed. The acceptance criteria of chore(roadmap): count cached tokens in the step budget #4387 ask for a report-only change (no file under src/), docs/ROADMAP_TOKEN_BUDGET.md, the cache share of a stage plan run, a verdict on the prompt order of the stage planner, and one issue URL for each recommendation. This pull request changes 6 files under src/ and adds no report, and its comment says "Stage plan run: not measured in this change" and "The order of the stage planner prompt is not checked in this change".

For the owner: this pull request replaces the report-only acceptance criteria of #4387 with the fix that the measurements recommend. If you want #4387 to stay open for a report, change Closes #4387 to a reference before the merge.

Order with #4556

#4556 and this pull request change the same lines (the field step_token_ceiling, defaults.py, the table of the roadmap limits, and test_the_ceiling_is_twice_the_default_step_budget). #4556 now makes the ceiling int | None with the property token_ceiling. The pull request that merges second needs a rebase: keep token_ceiling and the new default values of this change.

@dan-petty
dan-petty marked this pull request as draft November 11, 2026 10:48
@dan-petty
dan-petty force-pushed the fix/4387-step-budget-new-tokens branch 2 times, most recently from a48d7be to 7221a9b Compare November 11, 2026 11:38
@dan-petty
dan-petty marked this pull request as ready for review November 11, 2026 12:09
@dan-petty
dan-petty marked this pull request as draft November 11, 2026 12:46
The token budgets of roadmap.implement added the input tokens of each
reply. pydantic-ai counts the prefix that the provider read from its
cache in input_tokens, and 98.6% of the input of the runner Jobs were
cache reads. The step, its ceiling and the item budget now count the
input that the cache did not serve and the output. The outcome, the
tier spend and the record implement.spend keep the cached input.

Closes #4387
@dan-petty
dan-petty marked this pull request as ready for review November 11, 2026 12:52
@dan-petty
dan-petty force-pushed the fix/4387-step-budget-new-tokens branch from 7221a9b to fda278d Compare November 11, 2026 12:52
@dan-petty
dan-petty enabled auto-merge (squash) November 11, 2026 12:57
@dan-petty
dan-petty merged commit 9605f55 into main Nov 11, 2026
34 of 35 checks passed
@dan-petty
dan-petty deleted the fix/4387-step-budget-new-tokens branch November 11, 2026 13:29
@dan-petty

Copy link
Copy Markdown
Owner Author

Review findings, second pass (head fda278dc)

(a) minor, src/devops_cli/config/defaults.py: the new defaults fit only the cached hosted route. CONFIRMED

Evidence:

  • The default ladder is {'edit': ['devops-coder'], 'repair': ['devops-coder']}, and k8s/llm/gateway/config.yaml maps devops-coder to ollama_chat/qwen3.8:27b.
  • Prometheus (read only), 14 days to 2026-11-11, end_user="devops-cli-job", by model (litellm_requests_metric_total, litellm_input_tokens_metric_total, litellm_provider_cache_read_input_tokens_metric_total, litellm_output_tokens_metric_total):
Route Requests Input tokens for each request Cache reads for each request Output tokens for each request New tokens for each request
cached hosted (deepseek/deepseek-v4.1-flash-1017:floor) 4911 83669 82183 442 about 1930
local qwen3-coder:30b (the runner Jobs until 2026-11-05) 14642 39274 no series 201 about 39500
  • Only the DeepSeek route has a series of cache reads. qwen3.8:27b, qwen3-coder:30b and claude-sonnet-5-5 have none.
  • The new test on the old head: with the default settings, a step on the local route made 26 requests (assert 26 >= 200). With the old count of all input tokens and the old default of 8000000, it made about 200.

Change:

  • The defaults keep their values from before this pull request: step_token_budget 8000000 and item_token_budget 20000000. The ceiling stays twice the budget (fix(roadmap): default the step ceiling to twice the budget #4556): 16000000. The unit stays new tokens.
  • A route that reports no cache reads counts each input token in both units. So a step on the default ladder keeps about 200 requests of 39300 tokens.
  • On the cached hosted route, 8000000 new tokens are about 4100 requests. The time limit of the step (5400 s, about 540 requests at 10 s each) stops the step first, at about USD 0.2. Before this pull request, the budget stopped it after about 96 requests. So the fix of chore(roadmap): count cached tokens in the step budget #4387 stays.
  • I did not add a budget for each tier. It adds settings and a rule for a step that moves from a local tier to a hosted tier, and the kind of a tier does not tell if its route reports cache reads (the hosted claude-sonnet-5-5 reports none). The design records this and the defaults of 1000000 as rejected alternatives.
  • Tests: test_the_default_step_budget_fits_a_route_with_and_with_no_cache_reads (no cache reads: at least 200 requests; cached: at least 540 requests), and test_the_defaults_fit_the_measured_spend_of_steps_and_items expects 8000000 and 20000000. docs/CONFIGURATION.md, tests/roadmap/test_roadmap_limit_classes.py and the extension line of docs/ROADMAP_WORKFLOW.md are the same as on main again.

(b) minor, src/devops_cli/roadmap/implement/implement_run.py: a record of the old image counts all its input for the new item budget. CONFIRMED

Evidence:

  • _item_spend returned new_tokens(self.earlier), and _RecordedSpend.cache_read_tokens is 0 for an older record. The new test on the old head: a record of 100100 input tokens with no unit and item_token_budget 1000 stopped the next Job with limit_reached (assert 'limit_reached' != 'limit_reached').
  • One more defect of the same cause: a Job of the new image that read an old record wrote one sum of both units. The Job after it then read the old input as new tokens again.

Change:

  • The record names the unit of its count. implement.spend holds new_tokens, the count of the item budget, and each Job adds its own new tokens to it. The item budget reads new_tokens and no longer computes the count from the totals.
  • A record that an older version wrote has no new_tokens, so its Jobs count 0 by the rule of feat(roadmap): implement keeps phase marks and the spend of a killed Job #4070 (a missing key takes its empty value). Its input tokens hold cache reads in an unknown share. The record keeps them for the report.
  • Tests: test_spend_survives_kill_a_record_of_an_older_version_counts_no_tokens_for_the_item_budget (it replaces ..._gives_its_spend, which expected the old total to stop the run), and test_spend_survives_kill_the_mark_of_a_passed_group_holds_the_spend checks new_tokens 226.

The two majors: still fixed

Live steps (a person). They replace the live steps of the first comment

#4556 merged first (1dfb6c8d), and this branch is rebased on it. I resolved the conflicts so: defaults.py keeps the deletion of DEFAULT_ROADMAP_IMPLEMENT_STEP_TOKEN_CEILING; the description of step_token_ceiling keeps the text of #4556 and says "new tokens (see step_token_budget)"; the main spec keeps the ceiling requirement of #4556, which states its unit as the unit of step_token_budget, so this change no longer modifies it; docs/ENV_VARS.md is generated again.

The plan of the orchestrator is safe with this change: keep the line roadmap.implement.step_token_budget: 12000000 until the new image runs, then delete it. The settings load with the old image (ceiling 16000000) and with the new image (the ceiling follows the budget: 24000000). On the DeepSeek ladder of the live ConfigMap, 12000000 new tokens are about 6200 requests, so the time limit stops a step first.

  1. This pull request merged as 9605f554 (head fda278dc, with the fixes above).
  2. Keep step_token_budget: 12000000 in the ConfigMap devops/devops-cli-config until the release that holds this pull request runs. Check the version: kubectl -n devops exec deployment/roadmap-service -- devops --version.
  3. Then delete the line:
    kubectl -n devops edit configmap devops-cli-config
    # In the key devops-cli.yaml, delete the line `step_token_budget: 12000000` under `roadmap:` / `implement:`.
    kubectl -n devops get configmap devops-cli-config -o jsonpath='{.data.devops-cli\.yaml}' | grep -c step_token_budget   # 0
    kubectl -n devops rollout restart deployment/roadmap-service
    
    The runner Jobs read the ConfigMap when they start. The defaults then apply: 8000000 new tokens for a step, a ceiling of twice the budget (16000000), and 20000000 for an item. If the active config.yaml that devops k8s deploy-stack --stack devops renders holds the key, remove it there too, or the next deploy-stack writes it again.
  4. Old records need no step. After the rollout, a continuation whose earlier Job wrote implement.spend with the old image counts 0 for that Job and starts a new count of new tokens.

Checks

  • uv run devops ci fail-to-pass --base origin/main on fda278dc (13 min 41 s): 12 tests fail on the base and pass on this branch, and tests/roadmap/implement/test_roadmap_implement_limits.py does not collect on the base (no new_tokens). The new tests above are in this set.
  • pytest tests/roadmap/implement tests/roadmap/test_roadmap_limit_classes.py tests/security/test_config_declarations.py tests/config tests/ai/client/test_roadmap_agent_run.py after the rebase on fix(roadmap): default the step ceiling to twice the budget #4556: 2009 passed.
  • uv run devops ci --check --only constructs,docs,openspec: pass. uv run devops docs generate --sync-readme: no further diff. The pre-push hook passed.
  • The archive is still the last commit. The code, test and document changes are in the first commit, and the spec, proposal, design and tasks changes are in the archive commit.
  • The PR body is shorter (860 characters above Closes), because the monitor refused 1175.

dan-petty added a commit that referenced this pull request Nov 11, 2026
The record `implement.spend` kept only the first request of each step,
and the trace drops its oldest model events in a long run, so no record
showed which step used the tokens or its largest request. Each entry
under `implement.spend.steps` now also holds the requests, the input and
output tokens, the repair rounds and `largest_request_tokens` of the
step. The budget count does not change. The cached and uncached split
waits for #4553, which adds `cache_read_tokens` to `StepOutcome` on the
same lines.

Closes #4587

Change:
`openspec/changes/archive/2026-11-11-4587-keep-the-tokens-of-each-implement/`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

scope/roadmap The roadmap jobs: intake, refine, stage, implement and the Service that runs them type/bug Type bug: the code does not do what it must

Projects

None yet

Development

Successfully merging this pull request may close these issues.

chore(roadmap): count cached tokens in the step budget

1 participant