Skip to content

fix(fim): set has_error on microdollar_usage for upstream errors#2581

Merged
chrarnoldus merged 2 commits intomainfrom
fix/fim-has-error-tracking
Apr 20, 2026
Merged

fix(fim): set has_error on microdollar_usage for upstream errors#2581
chrarnoldus merged 2 commits intomainfrom
fix/fim-has-error-tracking

Conversation

@kilo-code-bot
Copy link
Copy Markdown
Contributor

@kilo-code-bot kilo-code-bot bot commented Apr 18, 2026

Summary

FIM (fill-in-the-middle) requests to Mistral/Inception were never recording has_error = true in microdollar_usage, even when the upstream provider returned a 4xx/5xx HTTP response.

Root cause: parseMistralFimUsageFromString and parseMistralFimUsageFromStream had no access to the HTTP status code. The regular chat-completion parsers already mirror this pattern: the streaming parser initialises reportedError = statusCode >= 400, and the non-streaming parser includes statusCode >= 400 in the hasError expression.

Changes:

  • Added statusCode: number parameter to both FIM parse functions
  • Non-streaming: hasError: !json.model || statusCode >= 400 (mirrors parseMicrodollarUsageFromString)
  • Streaming: let reportedError = statusCode >= 400 (mirrors parseMicrodollarUsageFromStream)
  • countAndStoreFimUsage extracts usageContext.status_code ?? 0 and passes it to both parsers

Verification

  • Reviewed diff against equivalent logic in processUsage.ts (lines 647 and 760)
  • TypeScript: change is type-safe (new number param, boolean expression unchanged)

Visual Changes

N/A

Reviewer Notes

status_code on MicrodollarUsageContext is number | null; the ?? 0 fallback means a missing status code does not falsely set has_error. In practice status_code is always set before countAndStoreFimUsage is called (route.ts:249).

FIM parse functions never checked the HTTP status code, so 4xx/5xx
responses from Mistral/Inception never set has_error=true in
microdollar_usage. Mirror the pattern used by regular chat completions:
initialise reportedError from statusCode>=400 in the streaming path,
and include statusCode>=400 in the hasError expression for non-streaming.
@kilo-code-bot
Copy link
Copy Markdown
Contributor Author

kilo-code-bot bot commented Apr 18, 2026

Code Review Summary

Status: No Issues Found | Recommendation: Merge

Files Reviewed (1 files)
  • apps/web/src/lib/ai-gateway/llm-proxy-helpers.ts

Reviewed by gpt-5.4-20260305 · 245,776 tokens

@chrarnoldus chrarnoldus merged commit 1652c90 into main Apr 20, 2026
16 checks passed
@chrarnoldus chrarnoldus deleted the fix/fim-has-error-tracking branch April 20, 2026 07: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.

3 participants