LangChain Tutorial (Python): adding cost tracking for intermediate developers

By Cyprian AaronsUpdated 2026-04-21
langchainadding-cost-tracking-for-intermediate-developerspython

This tutorial shows you how to add cost tracking to a LangChain Python app so every LLM call records token usage and estimated spend. You need this when you’re building agents or workflows that can make multiple model calls per request and you want visibility into what each run costs before it hits production.

What You'll Need

  • Python 3.10+
  • langchain
  • langchain-openai
  • openai API key
  • A valid OpenAI model name, like gpt-4o-mini
  • Basic familiarity with LangChain chains, prompts, and runnables

Install the packages:

pip install langchain langchain-openai openai

Set your API key in the environment:

export OPENAI_API_KEY="your-key-here"

Step-by-Step

  1. Start with a simple chain that makes one model call. This gives you a baseline before adding any accounting logic.
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI

prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a concise assistant."),
    ("user", "Summarize this in one sentence: {text}")
])

llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
chain = prompt | llm

result = chain.invoke({"text": "LangChain helps developers compose LLM applications."})
print(result.content)
  1. Wrap the invocation in LangChain’s built-in callback handler for token usage. get_openai_callback() captures prompt tokens, completion tokens, total tokens, and estimated cost for OpenAI-backed calls.
from langchain_community.callbacks import get_openai_callback
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI

prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a concise assistant."),
    ("user", "Explain this in plain English: {text}")
])

llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
chain = prompt | llm

with get_openai_callback() as cb:
    result = chain.invoke({"text": "LangChain tracks usage across model calls."})

print(result.content)
print(f"Prompt tokens: {cb.prompt_tokens}")
print(f"Completion tokens: {cb.completion_tokens}")
print(f"Total tokens: {cb.total_tokens}")
print(f"Estimated cost: ${cb.total_cost:.6f}")
  1. Use the same pattern around multiple steps so you can see cumulative spend across an entire workflow. This is the part most teams miss: one user request may trigger several LLM calls, and you want the total, not just per-call numbers.
from langchain_community.callbacks import get_openai_callback
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI

summarize_prompt = ChatPromptTemplate.from_messages([
    ("system", "You summarize text."),
    ("user", "Summarize: {text}")
])

rewrite_prompt = ChatPromptTemplate.from_messages([
    ("system", "You rewrite text for clarity."),
    ("user", "Rewrite this summary for a business audience: {summary}")
])

llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
summarize_chain = summarize_prompt | llm
rewrite_chain = rewrite_prompt | llm

with get_openai_callback() as cb:
    summary = summarize_chain.invoke({"text": "LangChain makes it easier to build LLM apps with composable chains."})
    final_text = rewrite_chain.invoke({"summary": summary.content})

print(final_text.content)
print(f"Workflow total tokens: {cb.total_tokens}")
print(f"Workflow estimated cost: ${cb.total_cost:.6f}")
  1. Put the tracking behind a small helper so your application code stays clean. In production, this is where you’d also send metrics to logs, Prometheus, Datadog, or your internal billing system.
from langchain_community.callbacks import get_openai_callback

def run_with_cost_tracking(chain, inputs):
    with get_openai_callback() as cb:
        output = chain.invoke(inputs)

    metrics = {
        "output": output.content,
        "prompt_tokens": cb.prompt_tokens,
        "completion_tokens": cb.completion_tokens,
        "total_tokens": cb.total_tokens,
        "estimated_cost_usd": cb.total_cost,
    }
    return metrics

# Example usage:
# metrics = run_with_cost_tracking(chain, {"text": "Track costs for every request."})
# print(metrics)
  1. If you need visibility per request in an API service, log the metrics with a request ID. That gives you enough detail to trace expensive requests back to specific users, prompts, or agent paths.
import uuid
import logging
from langchain_community.callbacks import get_openai_callback

logging.basicConfig(level=logging.INFO)

request_id = str(uuid.uuid4())

with get_openai_callback() as cb:
    result = chain.invoke({"text": "Explain why cost tracking matters."})

logging.info(
    {
        "request_id": request_id,
        "prompt_tokens": cb.prompt_tokens,
        "completion_tokens": cb.completion_tokens,
        "total_tokens": cb.total_tokens,
        "estimated_cost_usd": cb.total_cost,
        "response_preview": result.content[:120],
    }
)

Testing It

Run the script locally and confirm you get both the model output and non-zero token counts. If total_cost stays at 0, check that you’re using an OpenAI model supported by LangChain’s pricing metadata and that your callback wrapper actually surrounds the .invoke() call.

For multi-step workflows, verify that the totals reflect all calls inside the same with get_openai_callback() block. The easiest sanity check is to compare a one-call chain against a two-call workflow; the second should always show higher token usage and cost.

If you’re wiring this into an API, send a few requests with different input lengths. Longer prompts should produce higher prompt token counts, which is how you know the tracker is capturing real usage instead of static values.

Next Steps

  • Add per-user and per-endpoint cost aggregation in your database or observability stack.
  • Track costs for tool-calling agents separately from simple chains.
  • Learn how to combine callbacks with LangSmith so you can correlate traces with spend.

Keep learning

By Cyprian Aarons, AI Consultant at Topiax.

Want the complete 8-step roadmap?

Grab the free AI Agent Starter Kit — architecture templates, compliance checklists, and a 7-email deep-dive course.

Get the Starter Kit

Related Guides