Repository navigation
fix(proxy): raise on provider count_tokens errors instead of falling back to tiktoken - #46121
Conversation
…back to tiktoken Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
|
I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".
|
|
|
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 9576dcd32b
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| response: Final = _count(gateway, body) | ||
| assert _payload(response) == {"input_tokens": local}, response.text | ||
| assert response.status_code == status, response.text | ||
| assert "input_tokens" not in response.text, response.text |
There was a problem hiding this comment.
Assert the propagated provider message
When the provider rejects a request, this still passes if LiteLLM replaces its message with a generic error. Assert REJECTION instead
AGENTS.md reference: AGENTS.md:L28-L28
Useful? React with 👍 / 👎.
| assert response.request_model == "claude-test" | ||
| assert response.model_used == "claude-3-sonnet-20240229" | ||
| assert response.total_tokens > 0 | ||
| # The provider counter is engaged: its API error surfaces instead of a local count |
There was a problem hiding this comment.
Remove the redundant test comment
Remove this narration because the test name and assertion already explain it, while repository rules permit comments only for exceptional cases
AGENTS.md reference: AGENTS.md:L1-L7
Useful? React with 👍 / 👎.
| ), | ||
| call_endpoint=True, | ||
| ) | ||
| with patch("litellm.token_counter") as mock_local_counter: |
There was a problem hiding this comment.
Avoid patching LiteLLM's local counter
Remove this LiteLLM function patch. The raised provider error already proves the fallback path was skipped without doubling our own code
AGENTS.md reference: tests/unit/AGENTS.md:L17-L17
Useful? React with 👍 / 👎.
Merging this PR will not alter performance
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Keep #43900, #45301, #46121, #46784, #46759, #46767, #46772, #46815, #47057 and #46777. Port only the Gemini prerequisites from #46430. Remove the deferred Bedrock Mantle behavior and unrelated picks from the final tree. Restore the PDF test provider fixture and deployment, and retain required test imports.
Defer the breaking error policy from #46121. Adapt the #46777 count settings flow to attempt a labelled local estimate for provider failures across providers and configured count models. Keep provider errors when local counting is disabled or cannot estimate the request, and test the shared behavior across HTTP error statuses.
TLDR
Problem this solves:
How it solves it:
Intentional product change: when a provider's count_tokens API is called and fails,
/v1/messages/count_tokens,/v1/responses/input_tokensand the Gemini countTokens route return that error instead of a local tiktoken count. Callers who relied on always getting a number now get a 4xx or 5xxUser Flow
Before: a Claude Code user behind the proxy gets a made-up token count when the Anthropic call fails
{"input_tokens":11}, a tiktoken estimate, while the real Anthropic count is 14After: the same request returns Anthropic's own error
authentication_error"invalid x-api-key"Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
uv run pytest tests/unit/<your_test_file>.py -v. Leave the suites (make test-unit-*,make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or moreDelays in PR merge?
If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).
Screenshots / Proof of Fix
Config, real Anthropic API, one deployment with a working key and one with a bogus key
Before (aa52927)
Working key
curl -sS -w "\nHTTP %{http_code}\n" http://localhost:4100/v1/messages/count_tokens -H "Authorization: Bearer sk-1234" -H "Content-Type: application/json" -d '{"model":"claude-good","messages":[{"role":"user","content":"count these tokens please"}]}'Bogus key
"model":"claude-bad"After (9576dcd)
Working key
curl -sS -w "\nHTTP %{http_code}\n" http://localhost:4101/v1/messages/count_tokens -H "Authorization: Bearer sk-1234" -H "Content-Type: application/json" -d '{"model":"claude-good","messages":[{"role":"user","content":"count these tokens please"}]}'Bogus key
"model":"claude-bad"Type
🐛 Bug Fix
Caveats (if any)
Severe
Medium
Low
disable_token_counternow only matters for providers without a counting APIlitellm.acount_tokensis unchanged and still falls back locallyFinal Attestation
Link to Devin session: https://app.devin.ai/sessions/bd6127a6332c497a88fb4006154cf458
Open in Devin Desktop: https://app.devin.ai/desktop/session/bd6127a6332c497a88fb4006154cf458?variant=devin
Requested by: @krrish-berri-2