* fix: correct cache_hit_rate calculation and remove duplicate emission
Two bugs in prompt_cache_metrics telemetry:
1. Duplicate emission: prompt_cache_metrics was emitted twice per API
request — once using `actualInputTokens` and again using
`fullUsage.prompt_tokens`. This doubled all event counts in PostHog
and produced conflicting values.
2. Wrong denominator: cache_hit_rate was calculated as
`cacheReadTokens / prompt_tokens`, but the Anthropic adapter maps
`prompt_tokens` to only non-cached input tokens (`input_tokens`),
excluding cache reads and writes. When caching works well, this
produces ratios >> 1 (observed max: 89,892). The correct total is
`prompt_tokens + cache_read_tokens + cache_write_tokens`.
Fix: remove the first duplicate emission and compute total_prompt_tokens
as the sum of all three token types. cache_hit_rate is now a proper 0-1
ratio.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* fix: emit tool call id/name from tool-input-start in Vercel stream converter
The Vercel AI SDK streams tool calls as tool-input-start → tool-input-delta
→ tool-input-end → tool-call. Previously, tool-input-start was ignored (returned
null) and tool-call emitted the full tool call at the end, which meant streaming
consumers never saw the tool call id on intermediate chunks.
Now tool-input-start emits the initial chunk with id and function name (matching
OpenAI's streaming format), and tool-call returns null to avoid duplicating args
already streamed via tool-input-delta.
Generated with [Continue](https://continue.dev)
Co-Authored-By: Continue <noreply@continue.dev>
---------
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Co-authored-by: Continue <noreply@continue.dev>
Skip addCacheControlToLastTwoUserMessages when cachingStrategy is "none"
so requests truly have no caching markers. Updates test to verify.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add required `provider: "anthropic"` to AnthropicApi constructor calls
and use `(textPart as any)` type assertion for `cache_control` property
access, which is dynamically added and not in the ContentBlockParam type.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Cache the last two user messages in _convertBody() by calling
addCacheControlToLastTwoUserMessages() after applying the caching
strategy. This brings the direct Anthropic path to parity with
OpenRouter, which already caches user messages.
Caching the last two user messages allows Anthropic to serve ~90%
of those tokens from cache reads at 1/10th the cost, significantly
reducing per-request input token costs for multi-turn conversations.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Preserve Vitest's default excludes (dist, node_modules, etc.) when
adding the live test exclusion pattern.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Live test files (*.live.test.ts) make real Anthropic API calls and are
intended for manual validation only. Exclude them from the default
vitest configuration to prevent flaky CI failures.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Clone message content blocks before mutating for cache_control
(prevents mutation of original input body)
- Add `void` prefix to async posthogService.capture call
- Fix duplicate assistant messages in Scenario 3 test loop
- Add unit test verifying original body is not mutated
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The systemAndTools caching strategy now adds cache_control to the last
two user messages via addCacheControlToLastTwoUserMessages(). Update
existing test expectations to include the cache_control field.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The OpenAI PromptTokensDetails type doesn't include cache_read_tokens
(it's an Anthropic extension). Add 'as any' casts to fix TypeScript
build errors in the battle test file.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
7 distinct scenarios covering tool use round-trips, parallel tool calls,
long 8-turn conversations, large tool results (~200 lines), cache
invalidation on system message changes, identical request replays, and
multi-step agentic workflows with chained tool calls.
All scenarios validate cache hit rates >90% where expected and proper
cache misses when the system message changes.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Add addCacheControlToLastTwoUserMessages call in systemAndToolsStrategy
so the CLI path caches conversation turns (matching VS Code extension behavior)
- Report prompt_cache_metrics to PostHog with cache_read/write tokens and hit rate
- Add live API integration test validating cache writes on turn 1 and 99%+ hit
rates on subsequent turns (guarded by ANTHROPIC_API_KEY env var)
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* Add latest model definitions across providers
Add new models for OpenAI (gpt-4.1, gpt-5.2, codex-mini, o1/o3/o4-mini),
Gemini (2.5 stable, 2.5-flash-lite, 3-flash-preview), Mistral (devstral,
magistral, ministral, mistral-nemo), xAI (grok-4-fast, grok-4-1-fast,
grok-3-fast), and Bedrock (Claude 4.5/4.6, Nova, Llama 4, DeepSeek).
Update tool support and reasoning detection for the new model families.
* Add Z.ai (GLM) provider, Kimi K2/K2.5 models, and fix OpenRouter tool support bug
- Add Z.ai as a new provider with GLM-5, GLM-4.7, GLM-4-plus, GLM-4.5 models
- Add Kimi K2 and K2.5 model packages to Moonshot provider
- Fix variable shadowing bug in OpenRouter tool support where loop
variables shadowed the outer model parameter, causing specificModels
and supportedContains checks to always return true
- Add moonshotai/kimi and zai-org/glm to OpenRouter supported prefixes
- Add Z.ai and Moonshot to docs navigation
* Clean up unnecessary import aliases for zAI
* Add Z.ai Coding Plan endpoint note to docs
* Replace placeholder zai.png icon with proper zAI.svg
* Add zAI provider to openai-adapters
* Fix Z.ai API key URL
Add claude-sonnet-4-6 and claude-opus-4-6 as the new default Sonnet and
Opus models across providers, GUI configs, schemas, cost calculation,
and CLI defaults. Update display names for all >= 4.5 models to use
name-first format (e.g. "Claude Sonnet 4.5" not "Claude 4.5 Sonnet").