All guides

LangChain LLM Cost Tracking

Track LangChain model calls, chains, tool usage, retries, and agent runs in Cloptima.

8 min read Updated June 2026Connect your AI stack
On this page
  1. 01Point ChatOpenAI at Cloptima
  2. 02Track runs, not just requests
  3. 03Control expensive workflows
  4. 04Analyze cost drivers

01

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-5.4-mini",
    base_url="https://api.cloptima.ai/v1/ai",
    api_key="clop_vk_dPXO67p…",
    default_headers={"x-cloptima-team": "research", "x-cloptima-app": "agent"},
)

02

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.

03

Control expensive workflows

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

04

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