AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

ChatMistralAI Does Not Populate Standardized Usage_metadata, Causing LangSmith To Ignore Cached_tokens And Overcharge

ChatMistralAI returns token usage only in response_metadata.token_usage, which is deprecated and not parsed by LangSmith for cached token pricing. As a result, LangSmith treats all input tokens as uncached, leading to significant cost overestimation when using prompt caching.

highConfidence 85%LangchainAffected V1.1.5Fixed V1.1.6

Origin Analysis

The ChatMistralAI integration does not map the Mistral API's usage fields (including prompt_tokens_details.cached_tokens) to LangChain's standardized AIMessage.usage_metadata. LangSmith relies on usage_metadata for cost calculation; the provider-specific response_metadata.token_usage is ignored or deprecated.
1. Install langchain-mistralai 1.1.5 and langsmith 0.7.32.\n2. Set up a Mistral API key and LangSmith environment.\n3. Run the provided script that invokes ChatMistralAI sequentially with prompt_cache_key and compares actual cost (using cached token pricing) against LangSmith's calculated cost.\n4. Observe that LangSmith reports higher input cost because it does not subtract cached tokens; the overcharge percentage is printed.

Fixing Code Block

Edge Case Audit

Adding usage_metadata may cause double-counting if downstream code also reads response_metadata.token_usage; ensure consumers prefer usage_metadata. The change is backward-compatible but must be tested with both streaming and non-streaming invocations. If rolled back, LangSmith will again ignore cached tokens. No concurrency issues are introduced as this is per-message metadata. Users on older langchain-core versions that do not support usage_metadata will ignore the new field; verify compatibility with langchain-core >= 0.2.

Ecosystem Topology