Skip to content

fix(chat): clamp negative token counts in request log row - #101

Open
ngo-software wants to merge 1 commit into
Continuum-AI-Corp:mainfrom
ngo-software:fix/clamp-negative-token-counts
Open

ngo-software wants to merge 1 commit into
Continuum-AI-Corp:mainfrom
ngo-software:fix/clamp-negative-token-counts

Conversation

@ngo-software

@ngo-software ngo-software commented Sep 14, 2026

Copy link
Copy Markdown

Orca-Code-Review — push 1

Severity Count
P0 0
P1 0
P2 0
P3 0

✅ no blocking findings

Some upstream providers return negative token counts in malformed usage data. The cost calculation already clamps via _compute_cost_microcents, but the raw input_tokens / output_tokens columns in RequestLog were unprotected.

Negative values persisted to the DB corrupt analytics dashboards — negative total tokens, negative spend aggregates, broken percentile calculations.

Fix: clamp both fields to max(0, value) at extraction time in _build_log_row, so the log row never carries negative counts regardless of upstream data quality.

Tests: tests/unit/test_negative_token_clamping.py — verifies negative input tokens, negative output tokens, and both-negative cases all clamp to 0.

Some upstream providers return negative token counts in malformed usage
data. The cost calculation already clamps via _compute_cost_microcents,
but the raw input_tokens/output_tokens columns in RequestLog were
unprotected — negative values persisted to the DB corrupt analytics
dashboards (negative total tokens, negative spend aggregates).

Clamp both fields to max(0, value) at extraction time so the log row
never carries negative counts regardless of upstream data quality.

@orcacode-review orcacode-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🐳 OrcaCode Review

No findings — nothing to flag in this PR. Great work!

OrcaCode Review — Route Smarter. Ship Safer. Spend Less.
Engine-reported: 346 calls · 19.2M tokens · 98% cached

❤️ Share · Install OrcaCode Review

Free on GitHub — the review runs on your own OrcaRouter key. If it helped, a shout-out goes a long way.

Share: X · Reddit · LinkedIn
Follow: X · Discord · LinkedIn · OrcaRouter

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.

1 participant