Skip to main content
This guide shows you how to implement cost tracking in your application using ABV’s SDKs. Choose your preferred language and integration method below.
Before you start: Decide whether to ingest or infer usage data. See the Cost Tracking Reference to understand the differences.

Python SDK Implementation

Installation

Install the required packages:
The decorator approach is clean and integrates seamlessly with your existing functions.
OpenAI Usage Schema: When using OpenAI-style usage details, ABV automatically maps:
  • prompt_tokensinput
  • completion_tokensoutput
  • total_tokenstotal
  • prompt_tokens_details.*input_*
  • completion_tokens_details.*output_*

Method 2: Manual Context Manager

For more control, use the context manager approach:

JavaScript/TypeScript SDK Implementation

Installation

Install the required packages:

Setup

1. Add credentials to .env:
2. Create instrumentation.ts:
Important: Import instrumentation.ts as the first import in your application to ensure proper initialization.

Method 2: Observe Wrapper

Wrap existing functions to trace them automatically:

Method 3: Manual Span Creation

For maximum control, create spans manually:

Automatic Usage Capture via Integrations

Many ABV integrations automatically capture usage and cost from LLM responses. If you’re using an integration and usage data isn’t appearing as expected, contact support.
Check the integrations documentation to see which providers support automatic usage capture.

Ingesting Only Usage (Let ABV Calculate Cost)

You don’t need to calculate costs yourself. Simply ingest usage details, and ABV will calculate costs based on model definitions:

Next Steps

Custom Model Definitions

Learn how to define custom models and pricing

Daily Metrics API

Export usage and cost data for analytics

Common Issues

Check these:
  1. Verify you’re passing usage_details in the update call
  2. Ensure the model name matches a model definition in ABV
  3. For inference: Check if your model is supported in the model definitions list
  4. Look for errors in your application logs
Possible causes:
  1. Using inference instead of ingestion (switch to ingesting actual usage)
  2. Model pricing may have changed (update model definitions)
  3. Custom models need pricing configured
  4. For historical data: Model definitions aren’t applied retroactively
Solution: Reasoning models (like OpenAI o1) require ingested usage. ABV cannot infer usage because reasoning tokens are generated internally.Always ingest reasoning_tokens from the API response for these models.Learn more about reasoning models