Skip to main content

LangChain integration for Tokonomics — AI cost metering, budget alerts, and spending caps for LLM API calls.

Project description

langchain-tokonomics

LangChain integration for Tokonomics — automatic cost tracking, budget alerts, and spending caps for LLM API calls.

Features

  • Automatic cost tracking for every LangChain LLM call (OpenAI, Anthropic, Google, DeepSeek, Mistral, Cohere, xAI, and more)
  • 30+ models with up-to-date pricing (June 2026)
  • Budget alerts — get warnings when spending approaches a threshold
  • Per-model breakdowns — see which models cost the most
  • Thread-safe — works with async and parallel chains
  • No API key required — runs fully local by default
  • Optional Tokonomics API — for persistent tracking, team dashboards, and hard spending caps

Installation

pip install langchain-tokonomics

Quick Start

Track costs locally (no API key needed)

from langchain_openai import ChatOpenAI
from langchain_tokonomics import TokonomicsCallbackHandler

# Create handler with a $5 budget
handler = TokonomicsCallbackHandler(budget_usd=5.00)

# Use with any LangChain model
llm = ChatOpenAI(model="gpt-4o", callbacks=[handler])
response = llm.invoke("Explain quantum computing in 3 sentences.")

# Check costs
print(f"Cost: ${handler.total_cost:.6f}")
print(f"Tokens: {handler.total_tokens:,}")
print(handler.summary())

With budget warnings

def on_warning(spent, budget):
    print(f"WARNING: ${spent:.2f} of ${budget:.2f} budget used!")

handler = TokonomicsCallbackHandler(
    budget_usd=10.00,
    warning_threshold=0.8,  # Warn at 80%
    on_budget_warning=on_warning,
)

llm = ChatOpenAI(model="gpt-4o", callbacks=[handler])

for question in my_questions:
    if handler.tracker.budget_exceeded:
        print("Budget exceeded, stopping.")
        break
    llm.invoke(question)

With Tokonomics API (persistent tracking)

Sign up at tokonomics.ca for a free API key. This gives you a dashboard with historical cost data, team breakdowns, and hard spending caps.

handler = TokonomicsCallbackHandler(
    api_key="mk_your_api_key_here",
    budget_usd=50.00,
)

llm = ChatOpenAI(model="gpt-4o", callbacks=[handler])

Cost estimation without making API calls

from langchain_tokonomics.pricing import calculate_cost, get_pricing

# Check pricing for a model
pricing = get_pricing("gpt-4o")
print(f"GPT-4o: ${pricing['input']}/1M input, ${pricing['output']}/1M output")

# Estimate cost
cost = calculate_cost("gpt-4o", input_tokens=1000, output_tokens=500)
print(f"Estimated cost: ${cost:.6f}")

Per-model breakdown

handler = TokonomicsCallbackHandler()

# Run multiple models...
llm_4o = ChatOpenAI(model="gpt-4o", callbacks=[handler])
llm_mini = ChatOpenAI(model="gpt-4o-mini", callbacks=[handler])

llm_4o.invoke("Complex analysis task...")
llm_mini.invoke("Simple formatting task...")

# See cost breakdown
for model, cost in handler.cost_by_model().items():
    print(f"  {model}: ${cost:.6f}")

Supported Models

30+ models across 8 providers:

Provider Models
OpenAI GPT-4o, GPT-4o Mini, GPT-4.1, o3, o3-mini, o4-mini
Anthropic Claude Sonnet 4, Claude Opus 4, Claude Haiku 3.5
Google Gemini 2.5 Pro, Gemini 2.5 Flash, Gemini 2.0 Flash
DeepSeek DeepSeek V3, DeepSeek R1
Meta Llama 4 Maverick, Llama 4 Scout
Mistral Mistral Large, Mistral Small
xAI Grok 3, Grok 3 Mini
Cohere Command A

Unknown models fall back to GPT-4o Mini pricing.

Why Tokonomics?

LangChain makes it easy to build LLM applications, but production costs can spiral without visibility. langchain-tokonomics gives you:

  1. Real-time cost awareness during development and production
  2. Budget guardrails to prevent unexpected bills
  3. Model cost comparison to pick the right model for each task
  4. Team accountability when connected to the Tokonomics dashboard

Links

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

langchain_tokonomics-0.1.0.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

langchain_tokonomics-0.1.0-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

Details for the file langchain_tokonomics-0.1.0.tar.gz.

File metadata

  • Download URL: langchain_tokonomics-0.1.0.tar.gz
  • Upload date:
  • Size: 8.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for langchain_tokonomics-0.1.0.tar.gz
Algorithm Hash digest
SHA256 568f3a46d04a260a7cc827475dce64574a7d0c3710aefcd8fa1ae389a4714b9a
MD5 0d7058c9741d5b261827481c89d98551
BLAKE2b-256 495e575c2accd27a752f6354f85d9339c7e199f5ce9ea9a30dc64e038f9d902a

See more details on using hashes here.

Provenance

The following attestation bundles were made for langchain_tokonomics-0.1.0.tar.gz:

Publisher: publish.yml on aitoukhrib/langchain-tokonomics

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file langchain_tokonomics-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for langchain_tokonomics-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5e5a17db7737a0860445bb493b2a6c6d29fcd7872493064621fb99f7f2ec1410
MD5 bc978f17fda82debffc97c721e46b1ce
BLAKE2b-256 f6a0fef798602c6760064062fa9b112665ad3d439cc52a460facd8645ea92ccf

See more details on using hashes here.

Provenance

The following attestation bundles were made for langchain_tokonomics-0.1.0-py3-none-any.whl:

Publisher: publish.yml on aitoukhrib/langchain-tokonomics

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page