Back to GuidesAgents

LangChain LLM Cost Tracking

8 min readUpdated June 2026

Point ChatOpenAI at Cloptima

LangChain's ChatOpenAI accepts a base URL and default headers, so chains and agents route through Cloptima with no structural change.

python
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    model="gpt-4o-mini",
    base_url="https://api.cloptima.ai/v1/ai",
    api_key="clop_vk_dPXO67p…",
    default_headers={"x-cloptima-team": "research", "x-cloptima-app": "agent"},
)

Track runs, not just requests

A single user action can fan out into many model calls. Pass x-cloptima-session-id and x-cloptima-run-id (and agent-session/agent-run ids) per invocation so cost rolls up to the chain, tool, and agent run — not a flat request count.

Control expensive workflows

Apply stricter budgets to experimental agents, long-running tools, high-context prompts, and retry-heavy paths.

Analyze cost drivers

Break spend down by model, chain, tool, team, app, and environment to find the workflows that need prompt, routing, or architecture changes.

Put This Guide Into Practice

Cloptima automates the strategies described in this guide.

No credit card required
5-minute setup
Free trial