[bug-fix]return actual status code - /v1/messages/count_tokens endpoint#21352
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Greptile SummaryThis PR fixes a real bug where
Confidence Score: 4/5Not safe to merge as-is — dropping tools/system from count_tokens() silently breaks tool-use and system-prompt token counting for Anthropic Score is 4 because there is a confirmed P1 regression: tools=tools and system=system were accidentally dropped from the count_tokens() call inside the new try block. The error-handling fix itself is sound, but this side-effect must be corrected before merge. litellm/proxy/proxy_server.py lines 9121-9127 — restore tools=tools and system=system to the count_tokens() call
|
| Filename | Overview |
|---|---|
| litellm/proxy/proxy_server.py | Adds try/except for httpx.HTTPStatusError correctly, but accidentally drops tools and system parameters from the count_tokens() call, silently breaking tool-use and system-prompt token counting |
| tests/proxy_unit_tests/test_proxy_token_counter.py | Adds a well-structured mock test for the HTTPStatusError catch path; does not cover tools/system forwarding so the parameter regression goes undetected |
Sequence Diagram
sequenceDiagram
participant Client
participant ProxyServer
participant ProviderCounter
participant UpstreamAPI
Client->>ProxyServer: POST /v1/messages/count_tokens
ProxyServer->>ProviderCounter: count_tokens(model, messages, ...)
ProviderCounter->>UpstreamAPI: HTTP POST count tokens
UpstreamAPI-->>ProviderCounter: 429 / 403 HTTP error
ProviderCounter-->>ProxyServer: raise httpx.HTTPStatusError
Note over ProxyServer: [NEW] try/except catches error
ProxyServer->>ProxyServer: extract status_code from exception
ProxyServer-->>Client: ProxyException(code=429/403)
Note over ProxyServer: Before fix: uncaught → 500 returned
Greploops — Automatically fix all review issues by running /greploops in Claude Code. It iterates: fix, push, re-review, repeat until 5/5 confidence.
Use the Greptile plugin for Claude Code to query reviews, search comments, and manage custom context directly from your terminal.
Reviews (6): Last reviewed commit: "Merge branch 'litellm_ishaan_april6' int..." | Re-trigger Greptile
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
|
@greptile review with new commits. |
|
@greptile give it score out of 5 again with the new commits. |
Additional Comments (2)
After removing the manual status check, if Currently this is safe because Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
When This is a behavior change: previously, Vertex AI partner model HTTP errors would propagate as unhandled exceptions (resulting in 500s). Other providers (Anthropic, Bedrock, Azure AI) catch errors internally and return |
…hore/handle-count-tokens-errors
|
| GitGuardian id | GitGuardian status | Secret | Commit | Filename | |
|---|---|---|---|---|---|
| 29203064 | Triggered | Zapier Webhook URL | 151c76a | litellm/proxy/_experimental/out/_next/static/chunks/a7aecb91c09b0e9a.js | View secret |
| 29203060 | Triggered | Bearer Token | f3ed443 | tests/test_litellm/llms/anthropic/test_anthropic_common_utils.py | View secret |
| 29203066 | Triggered | Base64 Basic Authentication | f3ed443 | tests/litellm/test_no_hardcoded_secrets.py | View secret |
| 29203131 | Triggered | Generic High Entropy Secret | f3ed443 | tests/test_litellm/llms/anthropic/test_anthropic_common_utils.py | View secret |
| 29203090 | Triggered | Bearer Token | 151c76a | tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_debug.py | View secret |
| 29203092 | Triggered | Generic High Entropy Secret | 151c76a | tests/test_litellm/proxy/test_api_key_masking_in_errors.py | View secret |
| 29203053 | Triggered | Generic Password | f3ed443 | .circleci/config.yml | View secret |
🛠 Guidelines to remediate hardcoded secrets
- Understand the implications of revoking this secret by investigating where it is used in your code.
- Replace and store your secrets safely. Learn here the best practices.
- Revoke and rotate these secrets.
- If possible, rewrite git history. Rewriting git history is not a trivial act. You might completely break other contributing developers' workflow and you risk accidentally deleting legitimate data.
To avoid such incidents in the future consider
- following these best practices for managing and storing secrets including API keys and other credentials
- install secret detection on pre-commit to catch secret before it leaves your machine and ease remediation.
🦉 GitGuardian detects secrets in your source code to help developers and security teams secure the modern development process. You are seeing this because you or someone else with access to this repository has authorized GitGuardian to scan your pull request.
8b0fadf
into
BerriAI:litellm_ishaan_april6
|
|
Relevant issues
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
tests/litellm/directory, Adding at least 1 test is a hard requirement - see detailsmake test-unit@greptileaiand received a Confidence Score of at least 4/5 before requesting a maintainer reviewCI (LiteLLM team)
Branch creation CI run
Link:
CI run for the last commit
Link:
Merge / cherry-pick CI run
Links:
Type
🐛 Bug Fix
✅ Test
Changes
Return actual status code from count_tokens API - avoid returning 5XX from bad request errors
httpx.Response.raise_for_status() from
httpx async client postwas not being handled