dexcost

Quickstart

Start tracking AI agent costs in under 5 minutes.

Python today, more SDKs on the roadmap. v0.1.0 ships the Python SDK. TypeScript, Go, and Rust SDKs are in active development — follow the SDKs section for status.

Install the SDK

pip install dexcost

Requires Python 3.10+.

Initialize and set context

Three lines to start tracking every LLM call with customer attribution:

import dexcost

dexcost.init(api_key="dx_live_...")
dexcost.set_context(customer_id="acme-corp", project_id="chatbot")

# Every OpenAI/Anthropic call is now tracked automatically
response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Hello"}]
)

Group multiple calls into a task

When your agent makes multiple LLM calls per operation, group them:

dexcost.set_context(customer_id="acme-corp")

with dexcost.task("resolve_ticket") as t:
    # Step 1: Classify the issue
    classification = client.chat.completions.create(
        model="gpt-4o-mini", messages=[...]
    )

    # Step 2: Generate resolution
    resolution = client.chat.completions.create(
        model="gpt-4o", messages=[...]
    )

    # Step 3: Track non-LLM costs
    t.record_cost("pinecone", cost_usd="0.004")
    t.record_cost("stripe", cost_usd="0.029")
# All 4 costs are grouped under one task, attributed to acme-corp

Track non-LLM costs

Your agents call APIs, vector databases, and services that cost money too:

dexcost.set_context(customer_id="acme-corp")

with dexcost.task("search_and_answer") as t:
    # LLM call — auto-captured
    response = client.chat.completions.create(model="gpt-4o", messages=[...])

    # Non-LLM costs — manual
    t.record_cost("pinecone", cost_usd="0.004")
    t.record_cost("google_maps_api", cost_usd="0.005")
    t.record_cost("aws_lambda", cost_usd="0.0001", event_type="compute_cost")

Use with web frameworks

Set context once in middleware — every downstream call inherits it:

# FastAPI example
@app.middleware("http")
async def dexcost_middleware(request, call_next):
    dexcost.set_context(
        customer_id=request.state.user_id,
        project_id="api-v2",
    )
    return await call_next(request)

# All endpoints automatically have customer attribution
@app.post("/chat")
async def chat(request: ChatRequest):
    response = client.chat.completions.create(...)  # tracked!
    return response

See Multi-tenant context propagation for Django, Flask, and async-job patterns.

View your data

Your costs appear in the dexcost dashboard within seconds:

  • Overview — total spend, cost trends, top customers
  • Cost Health — retry costs, failure rates, cost efficiency
  • Cost Intelligence — which customers are profitable vs unprofitable
  • Customers — per-customer cost breakdown

Next steps

On this page