fix: HTTP verbose logging and anthropic provider usage fallback (#211)#212
fix: HTTP verbose logging and anthropic provider usage fallback (#211)#212
Conversation
Adding .gitkeep for PR creation (default mode). This file will be removed when the task is complete. Issue: #211
…lbacks (#211) The lazy log callback pattern (log.info(() => ({...}))) passed through the log-lazy npm package, adding indirection that could lose output when the CLI runs as a subprocess. Since the verbose check is already done at the top of the wrapper, lazy evaluation provides no benefit here. Changed all 5 HTTP log call sites to use direct calls (log.info('msg', data)) which write to stdout synchronously via the output() function. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
When using opencode provider with @ai-sdk/anthropic SDK, the standard AI SDK usage object is empty but providerMetadata.anthropic.usage contains valid token data with snake_case keys (input_tokens, output_tokens). Added fallback in getUsage() to extract tokens from anthropic metadata when standard usage is empty. Also extended the empty check to cover providers that set tokens to 0 instead of undefined. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Added 7 test cases covering: - Fallback to anthropic metadata when standard usage has undefined tokens - Fallback when standard usage has zero tokens - Cache read tokens extraction from anthropic metadata - Preference for standard usage when it has valid data - Graceful handling when anthropic metadata is missing - Preference for openrouter metadata over anthropic metadata Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Case study in docs/case-studies/issue-211/ includes: - Full session log (verbose-session-log.txt) - Root cause analysis for both HTTP logging and usage data issues - Timeline of events and comparison with upstream projects - Experiment scripts used during investigation Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This reverts commit 65668a6.
🤖 Solution Draft LogThis log file contains the complete execution trace of the AI solution draft process. 💰 Cost estimation:
Now working session is ended, feel free to review and add any feedback on the solution draft. |
🔄 Auto-restart 1/3Detected uncommitted changes from previous run. Starting new session to review and commit them. Uncommitted files: Auto-restart will stop after changes are committed or after 2 more iterations. Please wait until working session will end and give your feedback. |
…#211) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
🔄 Auto-restart 1/3 LogThis log file contains the complete execution trace of the AI solution draft process. 💰 Cost estimation:
Now working session is ended, feel free to review and add any feedback on the solution draft. |
✅ Ready to mergeThis pull request is now ready to be merged:
Monitored by hive-mind with --auto-restart-until-mergeable flag |
Summary
Fixes two issues reported in #211:
HTTP request/response logging not appearing in
--verbosemode: The lazy log callback pattern (log.info(() => ({...}))) passed through thelog-lazynpm package, adding indirection that could lose output when the CLI runs as a subprocess. Changed all 5 HTTP log call sites to use direct calls (log.info('msg', data)) since the verbose check is already done at the top of the wrapper."Provider returned zero tokens with unknown finish reason" error: When using opencode provider with
@ai-sdk/anthropicSDK, the standard AI SDK usage object is empty butproviderMetadata.anthropic.usagecontains valid token data (snake_case keys:input_tokens,output_tokens). Added an anthropic metadata fallback ingetUsage()to extract tokens from this metadata, similar to the existing OpenRouter fallback.Root Cause Analysis
See full case study:
docs/case-studies/issue-211/README.mdIssue 1: HTTP Logging
js/src/provider/provider.ts(verbose fetch wrapper)log.info(callback)→lazyLogInstance.info(wrappedCallback)→log-lazybit flag check →console.log()log.info('message', {data})which callsoutput()synchronouslyIssue 2: Empty Token Usage
js/src/session/index.ts(getUsage()function)opencode/minimax-m2.5-freemodel uses@ai-sdk/anthropicSDK with custom baseURLproviderMetadata.anthropic.usage(snake_case) but standardusageis emptyChanges
js/src/provider/provider.tsjs/src/session/index.tsgetUsage()js/tests/session-usage.test.tsjs/.changeset/fix-http-verbose-and-anthropic-usage.mddocs/case-studies/issue-211/experiments/js/package.jsonTest plan
🤖 Generated with Claude Code
Fixes #211