Your app's data
LiteLLM quickstart
Record LiteLLM calls with a custom callback and the Python SDK, or send LiteLLM's OpenTelemetry spans.
LiteLLM can reach AI Spendbook in three ways: the Python SDK's LiteLLM callback, LiteLLM's own OpenTelemetry export sent to the OTLP receiver, or the LiteLLM proxy connector, which reads the proxy's spend logs. The callback gives you control over every field, including the customer. None has been run against a live LiteLLM deployment.
Option 1: the SDK callback
Install the SDK and set SPENDBOOK_API_KEY and SPENDBOOK_ENDPOINT (https://www.aispendbook.com) as in the FastAPI quickstart. Then add the callback:
import litellm
import spendbook
from spendbook.integrations.litellm import SpendbookCallback
spendbook.init() # reads SPENDBOOK_API_KEY / SPENDBOOK_ENDPOINT
litellm.callbacks = [SpendbookCallback()]
litellm.completion(
model="anthropic/claude-haiku-5-5",
messages=[{"role": "user", "content": "Hello"}],
metadata={"customer_id": "acme", "feature": "support-bot"},
)# LiteLLM proxy: a module next to config.yaml
# spendbook_callback.py
import spendbook
from spendbook.integrations.litellm import SpendbookCallback
spendbook.init()
proxy_handler_instance = SpendbookCallback()
# config.yaml
litellm_settings:
callbacks: spendbook_callback.proxy_handler_instancecustomer_id,feature,environmentandrun_idcome from the request'smetadataor from tags such ascustomer:acme. LiteLLM'suserbecomes the end user.- Provider names are mapped to the bill they appear on:
azuretoazure_openai,bedrocktoaws_bedrock,vertex_aiandgeminitogoogle. Cache reads and writes are kept. - LiteLLM's
response_costis stored for comparison only, never used as spend. LiteLLM cache hits are not recorded. - Uncached input is
prompt_tokensminus cache reads and writes. On LiteLLM versions that do not count cache writes insideprompt_tokens, it is under-counted by the cache writes. - Don't also call
spendbook.instrument()for the same calls in the same process, or they are recorded twice.
Option 2: LiteLLM's OpenTelemetry export
# LiteLLM's own OpenTelemetry settings (LiteLLM reads these, not AI Spendbook)
OTEL_EXPORTER="otlp_http"
OTEL_ENDPOINT="https://www.aispendbook.com/api/v1/ingest/otlp/v1/traces"
OTEL_HEADERS="Authorization=Bearer afk_your_key"
# then turn on LiteLLM's otel callback
# SDK: litellm.callbacks = ["otel"]
# proxy: litellm_settings:
# callbacks: ["otel"]- These variables and the
otelcallback belong to LiteLLM. See LiteLLM's OpenTelemetry documentation for your version. If it expects header values URL-encoded, write the space afterBeareras%20. - The receiver accepts OTLP/HTTP only, with JSON or protobuf encoding and optional gzip. OTLP over gRPC is not served.
- A span becomes a usage event only when it carries a provider (
gen_ai.provider.name,gen_ai.system,llm.providerorllm.system), a model (gen_ai.response.model,gen_ai.request.modelorllm.model_name) and token counts (gen_ai.usage.input_tokensorgen_ai.usage.prompt_tokens,gen_ai.usage.output_tokensorgen_ai.usage.completion_tokens, or thellm.token_count.*names). Look at one of LiteLLM's spans and check it has them. - The provider name LiteLLM writes is stored under the bill's id (
bedrockasaws_bedrock,vertex_aiasgoogle). A model with no price sheet and no contract rate is stored without a cost. - LiteLLM doesn't write
spendbook.customer_id. To attribute spans, map an attribute LiteLLM does send under Settings, API keys, OTLP attribute mapping, send them through an OpenTelemetry Collector that sets it, or use option 1. - Spans with no
gen_ai.*orllm.*attribute are ignored. A span that has them but lacks a provider, model or token count is rejected and adds nothing.
Option 3: read the proxy's spend logs
For a self-hosted LiteLLM proxy, the LiteLLM proxy connector reads per-request spend logs with a master or admin key: model, provider, tokens, key, team, end user and tags. No code change is needed. It can also push AI Spendbook budgets to virtual keys as max_budget, off unless you turn it on. Use one option per call, or the same call is counted twice. Other gateways are on AI gateways and observability.
Check it works
- In the app, open Integrations. Under Add a source, find SDK / HTTP ingestion and choose Set up.
- Go to the Test event step. Use the key you just created, or paste the one your service uses, and choose Send test event.
- Wait for the Test event received notice. It means the key and the ingestion API work. If you see Accepted, but not visible yet, choose Send again after a minute. The test event has zero tokens, so it adds no cost, and it carries the feature
spendbook-connection-testso you can filter it out. - The test event checks your key. Then send one call through LiteLLM and open Usage Explorer: the call should appear with its model and token counts. With option 1, filter to the feature you passed in
metadata.