Skip to content

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

Closed

Description

@dan-petty

Problem

The step budget counts the input tokens of every request, added together. The context is resent in each request, so the number grows much faster than the amount of new text the model reads. If the model provider caches the shared prefix, most of those tokens cost little; if it does not, they cost the full price. We do not know which is the case, and the budget makes no difference between them. In #4004 the step stopped on 8,011,119 input tokens, but the real cost of the run is not known: the project price table has no price for deepseek-v4.1-flash-1017, so the spend counters record 0.

What to find out

  1. Does the model provider (through OpenRouter and the LiteLLM gateway) cache the shared prefix of requests for deepseek-v4.1-flash-1017? Check the provider documents and a measured pair of requests. Does the reply report cached tokens (prompt_tokens_details.cached_tokens or the provider field), and does the gateway keep that field in the usage that the CLI reads?
  2. The share of cached tokens in a real implement step and in a real stage plan run.
  3. Whether the budget should count uncached tokens, the cost, or the number of requests, and what limit then fits an item. State the effect on the stops of the last seven days (limit_reached and output_cap classes).
  4. The price of deepseek-v4.1-flash-1017 for the price table (input, cached input, output), so the spend counters show the real cost. Check the open price work (ai.price_overrides, fix(ai): an agent run on a hosted group is not local spend #3665) before proposing a change.
  5. Whether the order of the request (stable text first, changing text last) matters for the cache, and whether the prompts of the implement step and the stage planner follow it.

Done when

The answers to the five points, with the evidence, and a recommendation for the budget unit and the limit are in this issue. No code change is part of this issue; file a fix issue for each recommendation.

Related: #4004, roadmap.implement.step_token_budget, roadmap.implement.item_token_budget.

Proposed design

Problem statement

The step budget sums raw input tokens. It cannot tell cached from uncached tokens, and the project has no price for deepseek-v4.1-flash-1017. The spend counters show 0, and stops do not show the real cost. This item investigates provider caching, measures the cached share, and recommends a budget unit and limit.

Acceptance criteria

Requirement: No production code

The change SHALL add only the report and the OpenSpec change, and SHALL change no production code.

Scenario: Diff has report only

  • WHEN A test reads the pull request diff.
  • THEN The diff contains no file under src/.

Requirement: Provider cache evidence

The token-budget report SHALL state whether the provider caches the shared prefix and SHALL cite the provider documentation and one measured request pair.

Scenario: Cached field is recorded

  • WHEN A test reads docs/ROADMAP_TOKEN_BUDGET.md.
  • THEN The file names prompt_tokens_details.cached_tokens and gives two measured usage values for one shared prefix.

Requirement: Cached-token share

The report SHALL give the cached-token share for one real implement step and one real stage plan run.

Scenario: Both shares are present

  • WHEN A test reads the report.
  • THEN The file gives a percentage for the implement step and a percentage for the stage plan run.

Requirement: Budget unit and limit

The report SHALL recommend one budget unit and one limit, and SHALL state the effect on limit_reached and output_cap stops from the last seven days.

Scenario: Recommendation is complete

  • WHEN A test reads the report.
  • THEN The file names the chosen unit, the limit, and the count of each stop class.

Requirement: Model price

The report SHALL record the input, cached input and output prices for deepseek-v4.1-flash-1017 and SHALL check ai.price_overrides #3665.

Scenario: Three prices are present

Requirement: Prompt order

The report SHALL state the cache-aware prompt order and SHALL say whether the implement prompt and the stage planner follow it.

Scenario: Both prompts are checked

  • WHEN A test reads the report.
  • THEN The file states stable text first and gives a verdict for each prompt.

Requirement: Follow-up issues

The report SHALL list one follow-up issue for each recommendation.

Scenario: Each recommendation has a link

  • WHEN A test reads the report.
  • THEN Every recommendation has one issue URL.

Existing Tool Considered

No alternatives check has run. LiteLLM is the open, maintained gateway. It reports usage with cached-token details and computes cost from a model price map. It lacks a price for deepseek-v4.1-flash-1017; add that price through ai.price_overrides (#3665). The change writes a report and no production code.

Key questions

  • Does the provider cache the shared prefix for deepseek-v4.1-flash-1017, and does the gateway keep the cached-token field?: OpenRouter forwards provider usage. LiteLLM keeps prompt_tokens_details.cached_tokens in its usage object. The repository does not yet confirm that this model returns the field. The report must measure one repeated-prefix request pair. [sources: src/devops_cli/roadmap/implement/implement_step.py]
  • What is the cached-token share in a real implement step and in a real stage plan run?: No measured share is recorded. The stage comment already reports plan tokens. The implement step records request usage. The report must give one share for each real run. [sources: CONTEXT.md, src/devops_cli/roadmap/implement/implement_step.py]
  • Should the budget count uncached tokens, cost, or the number of requests, and what limit fits an item?: Count cost in USD per step and per item. Use uncached input tokens plus output tokens as the fallback when a price is missing. Set the limit from the measured seven-day stop data. Do not keep the raw input-token sum. (best practice) [sources: docs/ENV_VARS.md:127, docs/ROADMAP_WORKFLOW.md:196, src/devops_cli/roadmap/failure.py]
    • Chosen: Cost in USD. It bounds spend and applies the cached-input discount.
    • Rejected: Uncached input tokens. It avoids the price table, but it does not bound output spend.
    • Rejected: Number of requests. It ignores context size and the cached share.
    • Rejected: Raw input tokens. It overcounts cached tokens and hides the real cost.
  • What are the input, cached-input and output prices for deepseek-v4.1-flash-1017, and how does the price table take them?: The repository has no price for this model, so the spend counters record 0. The report must record the three provider prices. Add them through ai.price_overrides (fix(ai): an agent run on a hosted group is not local spend #3665), not a new table. [sources: src/devops_cli/config/settings.py]
  • Does the request order matter for the cache, and do the implement prompt and stage planner follow it?: Put stable text first and changing text last. The cache matches the longest common prefix. The repository does not record that either prompt follows this order. The report must check both prompts. [sources: src/devops_cli/roadmap/implement/implement_step.py, docs/ROADMAP_WORKFLOW.md]

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

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

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 informs the budget unit and limit decisions but blocks no user-facing feature.
    • Value: Medium. It clarifies real cost and the correct budget unit, which affects all future stop decisions.
    • Effort: Medium. It requires checking provider docs, measuring two request pairs, inspecting gateway usage fields, and writing a recommendation.
    • Related: chore(roadmap): inventory the start context of a step #4385, which asks for a different change. Both examine the step token budget and reference issue #4004, but #4385 inventories what fills the start context while the candidate investigates whether and how cached tokens should be counted in that budget.
  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 013c1f4 (see proposed design in issue body).

  6. added
    owner/agent
    on Nov 11, 2026
  7. dan-petty commented on Nov 11, 2026

    @dan-petty
    OwnerAuthor

    Service data, 48 hours to 2026-11-10T23:45Z: step budget stops

    This data answers point 2 and part of point 3 of this item.

    Count: 4 of 8 runner Jobs stopped on limit_reached against roadmap.implement.step_token_budget. The metric devops_cli_service_implement_failures_total{class="limit_reached"} shows 3, because a pod replacement near 14:30Z lost the #4004 increment.

    Time (UTC, 2026-11-10) Item Budget Input tokens Output tokens Result
    11:16:34 #3443 8M 8,050,668 43,618 status/blocked, work discarded
    12:10:28 #3451 8M 8,103,317 (+3,630,613 in group 1) 34,162 a person finished it in PR #4353
    14:29:52 #4004 8M 8,011,119 42,272 implement: retry finished it with 292,903 more tokens (PR #4378)
    20:36:20 #3429 12M 12,006,459 31,398 status/blocked

    First and last: 11:16:34Z and 20:36:20Z on 2026-11-10. Still occurring: yes. The last stop is in the last 6 hours of the window. After the window, Job devops-cli-runner-20261111-022552 (#3462) was at about 54 requests and 3.95M input tokens, with 99.2% cache reads.

    Cost: 82.7 Job minutes (19m16s + 22m52s + 21m27s + 19m06s), 39,802,176 input and 177,926 output tokens in about 380 requests. The gateway spend is only USD 0.162 (0.0349 + 0.0525 + 0.0343 + 0.0402), because the cache-read share of each stopped Job was 99.0%, 98.8%, 98.6% and 99.5%.

    Point 2, measured: over 48 hours, end_user="devops-cli-job" sent 58,482,776 input tokens, and 57,672,756 of them (98.6%) were provider cache reads (litellm_provider_cache_read_input_tokens_metric_total).

    Root cause: src/devops_cli/roadmap/implement/implement_limits.py:103 calls budget.add(response.usage.input_tokens). In pydantic-ai 2.59.0, UsageBase.input_tokens includes cache_read_tokens and cache_write_tokens. Each request sends the whole context again (about 100k to 140k tokens), so the budget is in effect a cap of about 60 to 120 requests, not a cap on cost or on new text. The raise from 8M to 12M (live at about 14:48Z) only moved the stop: #3429 stopped at 12,006,459 after about 120 requests. No commit on origin/main changes the budget unit.

    Point 3, input for the recommendation:

    Priority: this item is P2. The stops end 50% of the implement Jobs, and 2 P2 items (#3443, #3429) stay status/blocked behind them. By the blocker rule (a blocker of 2 or more P2 items is P1), the evidence suggests P1 for this item or for the fix issue that it files.

    Queries:

    Loki: {namespace="devops"} |~ "limit_reached|token_budget"   (2026-11-08T23:45Z to 2026-11-10T23:45Z)
    Loki: sum by (pod) (count_over_time({container="runner"}[52h]))   (8 runner Jobs)
    Prometheus: sum by (class) (increase(devops_cli_service_implement_failures_total{repo="dan-petty/devops-cli"}[48h]))
    Prometheus: litellm_input_tokens_metric_total{end_user="devops-cli-job"}, litellm_provider_cache_read_input_tokens_metric_total{end_user="devops-cli-job"}
    kubectl get jobs -n devops (start and completion times)
    
  8. 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.

  9. added
    status/in-progress
    status/in-review
    and removed
    status/ready
    status/in-progress
    on Nov 11, 2026
  10. dan-petty commented on Nov 11, 2026

    @dan-petty
    OwnerAuthor

    The two open points of the stage planner move to #4583: the cache share of a stage plan run (point 2) and the order of the stage planner prompt (point 5).

    PR #4553 replaces the report-only criteria of this item with the fix that the measurements recommend: the step budget, its ceiling and the item budget count new tokens. The PR comment of 2026-11-11 gives the answers to points 1, 2 (implement step), 3 and 4. A person decides if this replacement is accepted when the PR merges.

  11. added 2 commits that reference this issue on Nov 11, 2026
    2555f68
    0941cb5
  12. removed
    status/in-review
    owner/agent
    on Nov 11, 2026
  13. devops-cli-service commented on Nov 11, 2026

    @devops-cli-service
    Contributor
  14. dan-petty commented on Nov 11, 2026

    @dan-petty
    OwnerAuthor

    Correction to point 3 of comment 6790420886 on #4553: the 4 limit_reached stops used 1.1% to 1.9% of the merged default step budget of 8000000 new tokens. The 9% to 15% in that comment is for the rejected default of 1000000. Found in the adversarial check of #4553.

  15. devops-cli-service commented on Nov 11, 2026

    @devops-cli-service
    Contributor

    Live since 2026-11-11 14:13 UTC

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/mediumpriority/p2-mediumscope/roadmapsource/agenttype/chorevalue/medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions