Skip to content

fix(ocr): support token-priced billing - #39785

Closed
yujonglee-berri wants to merge 2 commits into
litellm_internal_stagingfrom
litellm_ocr_billing_foundation
Closed

yujonglee-berri wants to merge 2 commits into
litellm_internal_stagingfrom
litellm_ocr_billing_foundation

Conversation

@yujonglee-berri

@yujonglee-berri yujonglee-berri commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

TLDR

Problem this solves:

  • Token-priced OCR responses could record zero spend
  • Structured OCR output could drop token usage

How it solves it:

  • Normalizes page and token usage through one contract
  • Trusts provider metering instead of generated OCR content
  • Selects provider token rates for token usage

User Flow

Before: a developer receives successful Vertex DeepSeek OCR output, but the recorded spend can be zero

  1. They send POST http://localhost:4000/v1/ocr with a Vertex DeepSeek OCR model and document
  2. The response returns 200 with prompt and completion token usage
  3. They open http://localhost:4000/ui/?page=logs and see zero spend

After: the same request records provider-priced token spend

  1. They send the same POST http://localhost:4000/v1/ocr request
  2. The response returns 200 with preserved prompt and completion token usage
  3. They open http://localhost:4000/ui/?page=logs and see non-zero spend

Relevant issues

Linear ticket

Pre-Submission checklist

  • I have added meaningful tests
  • The focused test files covering my change pass locally
  • My PR passes all required CI/CD checks
  • My PR's scope is isolated to Python OCR billing
  • I have received a Greptile Confidence Score of at least 4/5

Screenshots / Proof of Fix

No live provider run was captured

Type

🐛 Bug Fix

Caveats (if any)

Low

Validation

  • OCR pricing and spend tests: 40 passed
  • Focused provider, logging, and cost tests: 21 passed
  • Repository pre-commit gate: passed

Final Attestation

  • The tests cover token, page, annotation, credit, and structured response billing

@codspeed

codspeed Bot commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing litellm_ocr_billing_foundation (6f79a95) with litellm_internal_staging (dd01abc)

Open in CodSpeed

@greptile-apps

greptile-apps Bot commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR adds token-priced OCR billing, centralizes OCR usage normalization, preserves provider usage for structured DeepSeek responses, and updates spend tracking and model pricing. The previous structured-response usage finding is fully fixed. Mapping-form structured content now has a validation fallback regression

Confidence Score: 4/5

The PR needs a consistent fallback for invalid mapping-form OCR content before it appears safe to merge

Mapping content is validated outside the existing ValidationError fallback, so malformed structured pages can turn an otherwise successful provider response into a failed OCR call. The previous structured-response usage issue is fully fixed because usage now always comes from the outer provider response

Files Needing Attention: litellm/llms/vertex_ai/ocr/deepseek_transformation.py

Important Files Changed

Filename Overview
litellm/llms/vertex_ai/ocr/deepseek_transformation.py Preserves outer provider usage but can reject malformed mapping content that previously fell back safely
litellm/llms/base_llm/ocr/transformation.py Adds shared token fields and normalization for OCR usage
litellm/cost_calculator.py Selects token pricing when split OCR token usage and token rates are available
litellm/proxy/spend_tracking/spend_tracking_utils.py Preserves token-based OCR usage while retaining page-billing defaults
litellm/litellm_core_utils/litellm_logging.py Normalizes OCR usage_info without treating unrelated response usage_info as token usage

Reviews (2): Last reviewed commit: "fix(ocr): harden token usage billing" | Re-trigger Greptile

@codecov

codecov Bot commented Sep 4, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 84.84848% with 10 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...tellm/proxy/spend_tracking/spend_tracking_utils.py 12.50% 7 Missing ⚠️
...ellm/llms/vertex_ai/ocr/deepseek_transformation.py 93.54% 2 Missing ⚠️
litellm/llms/base_llm/ocr/transformation.py 92.85% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@yujonglee-berri

Copy link
Copy Markdown
Contributor Author

@greptileai Please review the latest hardening commit, which preserves outer usage for structured OCR responses and centralizes usage normalization.

Comment on lines +51 to +52
if isinstance(content, Mapping):
return _DeepSeekOCRContent.model_validate(content)

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.

P1 Mapping content can fail

If provider content is a mapping with invalid page fields, validation escapes the fallback and fails an otherwise successful OCR response

Suggested change
if isinstance(content, Mapping):
return _DeepSeekOCRContent.model_validate(content)
if isinstance(content, Mapping):
try:
return _DeepSeekOCRContent.model_validate(content)
except ValidationError:
return None

Knowledge Base Used: Provider adapters and capabilities

@yuneng-berri
yuneng-berri deleted the branch litellm_internal_staging September 13, 2026 04:49
@yuneng-berri yuneng-berri reopened this Sep 13, 2026
@yujonglee-berri
yujonglee-berri deleted the litellm_ocr_billing_foundation branch September 16, 2026 17:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants