Quickstart
Initialize the TypeScript SDK and capture your first attributed LLM cost in under a minute.
One init() call at startup and a track() wrapper around your work — every LLM call inside the callback is captured automatically with customer attribution, token counts, latency, and cost.
Initialize
Call init() once at application startup. Pass your API key directly or set the DEXCOST_API_KEY environment variable:
import { init } from 'dexcost';
init({ apiKey: 'dx_live_...' }); // or set DEXCOST_API_KEY env varinit() patches the LLM provider SDKs that are installed in your environment and starts a background pusher that flushes buffered events to the Control Layer every 30 seconds.
Your first tracked call
Wrap your agent work in track(). LLM calls made inside the callback are auto-captured — use your OpenAI or Anthropic client exactly as you normally would:
import { init, track, closeAsync } from 'dexcost';
import OpenAI from 'openai';
init({ apiKey: 'dx_live_...' });
const openai = new OpenAI();
await track({ taskType: 'summarise_doc', customerId: 'acme-corp' }, async (task) => {
// LLM calls are auto-captured — no changes to the call site
const response = await openai.chat.completions.create({
model: 'gpt-4o',
messages: [{ role: 'user', content: 'Summarise this document' }],
});
// Record a non-LLM cost alongside the auto-captured LLM cost
task.recordCost('pdf_parser', 0.002);
});
await closeAsync();Once track() resolves, the task is closed and its aggregated cost — totalCostUsd, llmCostUsd, externalCostUsd, token counts — is written to the local SQLite buffer and synced to the Control Layer in the background.
Run it locally
Pass environment: "development" to init() to enable dev mode. In dev mode, every cost event is printed to stderr and no data is sent to the cloud — useful for local development and debugging:
import { init, track } from 'dexcost';
import OpenAI from 'openai';
init({ environment: 'development' });
const openai = new OpenAI();
await track({ taskType: 'summarise_doc', customerId: 'acme-corp' }, async (task) => {
const response = await openai.chat.completions.create({
model: 'gpt-4o',
messages: [{ role: 'user', content: 'Summarise this document' }],
});
});You can also set the DEXCOST_ENV=development environment variable instead of passing the argument to init().
Next steps
- Instrumentation — details on all 6 LLM providers, HTTP tracking, and how to limit which providers are instrumented.
- Configuration — all
init()parameters, environment variables, privacy options, and the local SQLite buffer.