Skip to content

Use record.provider in requestTokens for Anthropic and OpenAI token counts - #80794

Open
rabeckett wants to merge 1 commit into
mainfrom
fix/provider-aware-request-tokens
Open

rabeckett wants to merge 1 commit into
mainfrom
fix/provider-aware-request-tokens

Conversation

@rabeckett

Copy link
Copy Markdown
Collaborator

Fixes #80793. Part of #79102.

Summary

Passes record.provider from runCost into requestTokens in actions/setup/js/push_routing_history.cjs so non-cached input token counts match AWF's credit meter (gh-aw-firewall containers/api-proxy/guards/ai-credits/accounting.js) when inputTokensIncludeCache is absent from firewall.token_usage:

  • An explicit boolean used.inputTokensIncludeCache remains authoritative whenever AWF provides it.
  • Otherwise, provider === "anthropic" is treated as additive (does not subtract cached tokens), provider === "openai" is treated as inclusive (subtracts cached tokens, clamped at 0), and "copilot" (or any other/missing provider) retains the existing input >= cacheRead + cacheWrite heuristic.

Measured credits are unaffected; this improves repriced cross-model cost estimates for native Anthropic (Claude) and OpenAI (Codex) workflows.

Concrete examples

record.provider usage (input, cacheRead, cacheWrite, output, inputTokensIncludeCache) Before (non-cached input) After (non-cached input)
anthropic 20000, 0, 15000, 300, omitted 5000 20000
openai 23799, 23194, 0, 45, omitted 605 605
openai 382, 19712, 0, 69, omitted 382 0
copilot 23799, 23194, 0, 45, omitted 605 605
copilot 382, 19712, 0, 69, omitted 382 382
anthropic 20000, 0, 15000, 300, true 5000 5000

Changes

  • actions/setup/js/push_routing_history.cjs: Pass record.provider into requestTokens(used, provider) and apply provider-aware defaults when used.inputTokensIncludeCache is not a boolean.
  • actions/setup/js/push_routing_history.test.cjs: Add unit tests for anthropic (including input >= cacheWrite and explicit flag override), openai (input >= cacheRead, input < cacheRead clamped at 0, and explicit flag override), and copilot plus other/missing providers retaining the heuristic unless the flag is explicitly set.
  • docs/src/content/docs/reference/adaptive-routing.md: Update the token-counting note under "What is recorded".

Validation (head fa5e02f74a)

  • eslint-factory ESLint on push_routing_history.cjs and push_routing_history.test.cjs: passed
  • npx prettier --check push_routing_history.cjs push_routing_history.test.cjs: passed
  • npm run typecheck (tsc --noEmit): passed
  • npx vitest run push_routing_history.test.cjs: 23/23 passed

…ounts

Fixes #80793

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@rabeckett
rabeckett marked this pull request as ready for review November 24, 2026 20:18
Copilot AI adaptive review requested due to automatic review settings November 24, 2026 20:18
@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

✅ PR Specification Reviewer completed successfully!

PR Specification Reviewer

Outcome: reported_action

The agent completed and reported one or more actions. The no-op output is a completion signal, not a claim that no action was taken.

The [aw] No-Op Runs tracker omits agent messages. The category uses structured run status, not interpretation of agent prose.

No-op message

Reviewed PR #80794 at head fa5e02f against T-UAS-031 and T-UAS-081; no actionable specification finding or update proposal.

Generated by PR Specification Reviewer for #80794 · copilot · gpt56 · ~2 AIC · ⌖ ~2 AIC · ⊞ 1.8K · history

@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

✅ Ponytail Reviewer completed successfully!

Ponytail Reviewer

Outcome: reported_no_action

The agent completed and reported no action. A no-op output alone does not verify that all required work was completed or that publication was possible.

The [aw] No-Op Runs tracker omits agent messages. The category uses structured run status, not interpretation of agent prose.

No-op message

Lean already. Ship.

Generated by Ponytail Reviewer for #80794 · codex · routed: gpt61 medium · ~5 AIC · ⌖ ~1 AIC · ⊞ 176 · history

@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

✅ Test Quality Sentinel completed test quality analysis.

🧪 Test quality analysis by Test Quality Sentinel · copilot · routed: haiku55 xhigh · ~4 AIC · ⌖ ~2 AIC · ⊞ 7.4K · history

@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

✅ PR Code Quality Reviewer completed the code quality review.

PR Code Quality Reviewer

Outcome: reported_action

The agent completed and reported one or more actions. The no-op output is a completion signal, not a claim that no action was taken.

The [aw] No-Op Runs tracker omits agent messages. The category uses structured run status, not interpretation of agent prose.

No-op message

Completed PR #80794 review: submitted a no-finding review and recorded the outcome for head fa5e02f.

🔎 Code quality review by PR Code Quality Reviewer · copilot · gpt56 · ~3 AIC · ⌖ ~2 AIC · ⊞ 1.7K · history

@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

🧠 Matt Pocock Skills Reviewer has completed the skills-based review. ✅

🧠 Reviewed using Matt Pocock's skills by Matt Pocock Skills Reviewer · copilot · routed: haiku55 xhigh · ~6 AIC · ⌖ ~2 AIC · ⊞ 5.1K · history

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the changed lines in the provider-aware token accounting update. I found no actionable correctness, reliability, performance, or maintainability issues that should block merge. The explicit provider defaults are covered by the native Anthropic/OpenAI routing conventions, and the tests cover explicit overrides, missing providers, and the defensive OpenAI clamp. Specialist first-pass findings were independently adjudicated and none were retained.

🔎 Code quality review by PR Code Quality Reviewer · copilot · gpt50 · ~3 AIC · ⌖ ~2 AIC · ⊞ 1.7K
Comment /review to run again

@github-actions

Copy link
Copy Markdown
Contributor

Comment Memory

Peek at saved memory (spec-review-history)
{"id":"ldg-45aef263-a726-4104-8d71-46fd2b9c527e","parents":[],"payload":{"id":"ldg-ebbab59d-1378-447d-8821-cf96153c9c7c","operation":"append","value":{"finding_count":0,"head_sha":"fa5e02f74ada9a2a87403af43674f90d9450f099","outcome":"aligned"}},"sha":"sha256:c2dad73f9a4ebabe15416da316af77a98943af6ad2e7a3475a90a0af48305073","timestamp":"2026-11-24T20:26:10.375Z","type":"ledger_append","version":1}
History

@github-actions

Copy link
Copy Markdown
Contributor

Comment Memory

Peek at saved memory (review-history)
{"id":"ldg-24ab73d8-022c-4ec9-b860-60e085f66b3f","parents":[],"payload":{"id":"ldg-031e4f6e-d9eb-4e0a-94f3-96b4a84c63f0","operation":"append","value":{"finding_count":0,"head_sha":"fa5e02f74ada9a2a87403af43674f90d9450f099","outcome":"no_findings"}},"sha":"sha256:21e3583520e84a729ba47ebda3cf6f1f9f6b0646411d281d20fe70b915187a7e","timestamp":"2026-11-24T20:26:58.391Z","type":"ledger_append","version":1}
History

@github-actions

Copy link
Copy Markdown
Contributor

🧪 Test Quality Sentinel Report

✅ Test Quality Score: 90/100 — Excellent

Analyzed 1 test(s): 1 design, 0 implementation, 0 violation(s).

📊 Metrics (1 test)
Metric Value
Analyzed 1 (Go: 0, JS: 1)
✅ Design 1 (100%)
⚠️ Implementation 0 (0%)
Edge/error coverage 1 (100%)
Duplicate clusters 0
Inflation YES (21 test lines added vs. 8 production lines added, 2.6:1)
🚨 Violations 0
Test File Classification Issues
counts input without cache (record.provider for Anthropic and OpenAI, heuristic for Copilot) actions/setup/js/push_routing_history.test.cjs:185 behavioral_contract / high_value / design_test Inflation flag; weak provider isolation (see below)
⚠️ Flagged Tests (1)

counts input without cache (record.provider for Anthropic and OpenAI, heuristic for Copilot) (actions/setup/js/push_routing_history.test.cjs:185)

  • Inflation (2.6:1): the added lines are one assertion group per new requestTokens path (Anthropic additive and flag override, OpenAI clamp and flag override, Copilot heuristic, missing provider), so they are not padding. The formula awards 0 of 10 inflation points.
  • Weak provider isolation (low): the test name says the count reads record.provider, but the routing selection sets selectedProvider to the same value as the token record. A change that read the routing provider instead would still pass. Setting a different routing provider in one case would close this gap.
  • Method: the expected values were hand-traced against requestTokens in push_routing_history.cjs, not executed. Vitest is not installed in this sandbox.

Verdict

✅ passed. 0% implementation tests (threshold: 30%). No guideline violations.

🧪 Test quality analysis by Test Quality Sentinel · copilot · routed: haiku55 xhigh · ~4 AIC · ⌖ ~2 AIC · ⊞ 7.4K · history
Comment /review to run again

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Test Quality Sentinel: 90/100. 0% implementation tests (threshold: 30%). No guideline violations.

🧪 Test quality analysis by Test Quality Sentinel · copilot · routed: haiku55 xhigh · ~4 AIC · ⌖ ~2 AIC · ⊞ 7.4K
Comment /review to run again

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are identified for the provider-specific token-accounting changes.

No open issues 🎉

What changed in this PR

Updates adaptive-routing token accounting to use provider-specific defaults, improving cross-model cost estimates without changing measured credits.

Changes:

  • Uses Anthropic’s additive and OpenAI’s inclusive input-token conventions when no explicit cache flag is present; preserves the heuristic for other or missing providers.
  • Adds tests for provider defaults, explicit flag overrides, and zero-clamping.
  • Documents the provider-aware fallback behavior.
File Description
docs/​src/​content/​docs/​reference/​adaptive-routing.md Explains provider-specific token-counting defaults.
actions/​setup/​js/​push_routing_history.test.cjs Covers provider defaults, overrides, and fallback behavior.
actions/​setup/​js/​push_routing_history.cjs Passes record.provider into token accounting and applies the appropriate default.

Review effort: Balanced (adaptive)

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review summary

The provider-aware branches in requestTokens match the PR table and the new test expectations. The Copilot heuristic, explicit inputTokensIncludeCache override, and clamping at 0 are preserved. No other caller uses the old heuristic, and the fixture-based tests only use copilot, so they are unaffected.

One non-blocking readability suggestion is posted inline on line 176, where the nested ternary is hard to scan.

Verification

The vitest suite could not be installed in this sandbox because the npm registry failed TLS verification. Instead, I extracted requestTokens and checked every expectation from the new tests with Node, and all of them pass.

🧠 Reviewed using Matt Pocock's skills by Matt Pocock Skills Reviewer · copilot · routed: haiku55 xhigh · ~6 AIC · ⌖ ~2 AIC · ⊞ 5.1K
Comment /matt to run again

This branch has not been deployed

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use record.provider in requestTokens for Anthropic and OpenAI token counts

2 participants