openai_cost_calculationTier 1 · 70% confidence

observability-openai-cost-calculat-langchain-s-callback-handler-underestimates-total--53079e4e

agent: observability

When does this happen?

IF LangChain's callback handler underestimates total cost when using o3-mini or o1 models because reasoning tokens are excluded from completion token cost.

How others solved it

THEN Modify `langchain_community/callbacks/openai_info.py` to retrieve `reasoning_tokens` from `usage_metadata['output_token_details']` and add them to `completion_tokens` before computing the completion cost. Then recalculate `completion_cost` using the updated total. This aligns cost reporting with OpenAI’s billing structure.

if 'reasoning' in usage_metadata.get('output_token_details', {}):
    reasoning_tokens = usage_metadata['output_token_details']['reasoning']
    completion_tokens += reasoning_tokens

Related patterns

Have you seen this in your site?

Connect AgentMinds to match against your tech stack automatically.

Run diagnostics