Repository navigation
Conversation
…rices Claude Code prices every turn from its Anthropic price table. With ANTHROPIC_BASE_URL pointing at OpenRouter and the model aliases mapped to a non-Anthropic model, total_cost_usd is a guess at Anthropic prices for a model Claude Code does not know (modelUsage marks it costBasis "unknown") — measured ~25x too high for DeepSeek Flash. turn_cost() re-prices the result event from modelUsage (tokens per model, cumulative over every API call of the turn) against the provider's public price list, cached for 6h, and keeps the native figure whenever it cannot: no proxy, unknown proxy host, a model missing from the list, no modelUsage, or a failed price fetch. Wired into both stream parsers and the JSONL recovery path. Fixes Abilityai#3489 Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
merge-train (2026-10-10): not on this train — rides the next one once fixed. Validation (
Smaller notes, not blockers: |
|
✅ Nightly unit-suite clean when this PR is merged into |
|
merge-train (2026-10-11): ejected again. Nothing has been pushed since the 2026-10-10 note, so both points it raised are still open. This train also sets
The conflict with |
# Conflicts: # docker/base-image/agent_server/services/stream_parser.py # tests/registry.json
…e the last cost reader Review follow-up for Abilityai#3489: - _recover_metadata_from_raw_messages now prices through turn_cost, so a turn recovered from raw_messages no longer keeps the Anthropic-priced total_cost_usd. - turn_cost never does network I/O. The price list loads in a daemon thread at agent-server boot (prefetch_prices) and whenever it is stale; until then the native figure is kept. The lock only guards the cache. A failed refresh keeps the previous list and retries after 5 minutes. - _proxy_host re-runs build_execution_env only when .env or the ANTHROPIC_BASE_URL override changes, so its .env warnings no longer repeat on every result event. - An explicit cache price of 0 is a price, not a missing one. - Tests drive the production parsers (process_stream_line, _recover_metadata_from_jsonl, _recover_metadata_from_raw_messages), the no-blocking guarantee, and _fetch_openrouter_prices over a canned /api/v1/models payload. - docs: agent-runtime.md describes cost behind a proxy. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Thanks for the thorough review. All three points are addressed in f6c2219, and
Smaller notes: Written with Claude Code. |
|
merge-train (2026-11-04): re-validated at Blocked on a product ruling. ent#1001 has since closed the Findings to fix if it goes ahead:
Smaller points: a 200 with an empty |
|
Product ruling (2026-11-19): go ahead now, for container-baseline proxies only. Recording the wrong price for a turn is a correctness problem on its own, and it does not need to wait for #2982: the pricing logic keys on the proxy host the container already runs with, so it carries over unchanged when #2982 adds a sanctioned way to set that host. The scope stays as ent#1001 left it, with no @jjaeger94 that unblocks the three findings from the 11-04 note, which are now the whole list:
Please also say in the PR description that this covers container-baseline proxies only. |
Description
When a Claude Code agent runs through OpenRouter, Trinity records the turn cost at Anthropic prices (#3489). Claude Code computes
total_cost_usdfrom its own price table. For a model it does not know,modelUsagemarks that figure"costBasis": "unknown", and on our DeepSeek Flash agents it came out about 25× too high.This PR adds
agent_server/services/provider_pricing.pywith one function,turn_cost(result):ANTHROPIC_BASE_URLin the execution env (the samebuild_execution_env()the spawn uses, so it covers a.env-injected base URL). The host is re-read only when.envor theANTHROPIC_BASE_URLoverride changes.openrouter.aionly for now), it re-prices frommodelUsage: input, output, cache-read and cache-write tokens of every model the turn touched, againsthttps://openrouter.ai/api/v1/models.turn_costnever touches the network: it runs on the stdout reader thread. The list is loaded in a daemon thread at agent-server boot (prefetch_prices, startup hook inmain.py) and refreshed in the background every 6 h. A failed refresh keeps the previous list and retries after 5 min.total_cost_usdunchanged: no proxy, an unknown host, a model missing from the list, nomodelUsage, a price list that is not loaded yet, a failed fetch, or any exception. A plain Anthropic setup behaves exactly as before.total_cost_usd: both parsers instream_parser.py,_recover_metadata_from_jsonland_recover_metadata_from_raw_messages.Adding another provider means adding one entry to
_PRICE_SOURCES.Measured on a real turn: Trinity reported $0.311,
modelUsage× OpenRouter prices gives $0.0137, and the turn's generations summed via/api/v1/generationcost $0.0116. That sum leaves out side calls such as WebSearch, which are not in the stream, whilemodelUsagecounts them. After deploying this on v0.9.5, a one-word task loggedprovider pricing: openrouter.ai cost $0.005241 (native estimate $0.08736).The code was written with Claude Code.
Related Issue
Fixes #3489
Journey Impact
Journey Impact: none: agent-server cost bookkeeping only. No user flow, endpoint or UI changes, and the cost value keeps its meaning (USD per turn).
Type of Change
Testing
module under test unless its live consumer is CI itself (a workflow, a vendored copy, a
route table), and no bound check at the one value where both bounds coincide (bug(process): tests that assert source TEXT instead of executing the path — three green-CI ejections in one train, third run running #2829)
What ran:
dev):tests/unit/test_provider_pricing.py(19 tests) drivesprocess_stream_line, both recovery paths, the no-blocking guarantee and_fetch_openrouter_pricesover a canned payload. All unit tests touching the changed modules (59 files): 1666 passed locally on Python 3.13.tests/unit/test_provider_pricing.py(10 tests, new, registered intests/registry.json), together with the existing parser and recovery tests:test_claude_code_result_recovery,test_1870_completed_turn_recovery,test_1673_execution_error_not_success,test_claude_code_session_id_parser,test_1849_ede_diagnostic_filteredandtest_model_context_catalog. 215 passed on v0.9.5.trinity-agent-base:v0.9.5with two OpenRouter agents. Costs were checked against the OpenRouter key usage.🤖 Generated with Claude Code