# LiteLLM quickstart

Record LiteLLM calls with a custom callback and the Python SDK, or send LiteLLM's OpenTelemetry spans.

AI Spendbook documentation, Your app's data. Web page: https://www.aispendbook.com/docs/quickstart-litellm

LiteLLM can reach AI Spendbook in three ways: the [Python SDK](https://www.aispendbook.com/docs/python-sdk)'s LiteLLM callback, LiteLLM's own OpenTelemetry export sent to the [OTLP receiver](https://www.aispendbook.com/docs/opentelemetry), or the [LiteLLM proxy connector](https://www.aispendbook.com/integrations/litellm), 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](https://www.aispendbook.com/docs/quickstart-fastapi). Then add the callback:

```python
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"},
)
```

```python
# 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_instance
```

- `customer_id`, `feature`, `environment` and `run_id` come from the request's `metadata` or from tags such as `customer:acme`. LiteLLM's `user` becomes the end user.
- Provider names are mapped to the bill they appear on: `azure` to `azure_openai`, `bedrock` to `aws_bedrock`, `vertex_ai` and `gemini` to `google`. Cache reads and writes are kept.
- LiteLLM's `response_cost` is stored for comparison only, never used as spend. LiteLLM cache hits are not recorded.
- Uncached input is `prompt_tokens` minus cache reads and writes. On LiteLLM versions that do not count cache writes inside `prompt_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

```bash
# 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 `otel` callback belong to LiteLLM. See LiteLLM's OpenTelemetry documentation for your version. If it expects header values URL-encoded, write the space after `Bearer` as `%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.provider` or `llm.system`), a model (`gen_ai.response.model`, `gen_ai.request.model` or `llm.model_name`) and token counts (`gen_ai.usage.input_tokens` or `gen_ai.usage.prompt_tokens`, `gen_ai.usage.output_tokens` or `gen_ai.usage.completion_tokens`, or the `llm.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 (`bedrock` as `aws_bedrock`, `vertex_ai` as `google`). 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](https://www.aispendbook.com/docs/quickstart-otel-collector) that sets it, or use option 1.
- Spans with no `gen_ai.*` or `llm.*` 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](https://www.aispendbook.com/integrations/litellm) 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](https://www.aispendbook.com/docs/ai-gateways).

## Check it works

1. In the app, open **Integrations**. Under **Add a source**, find **SDK / HTTP ingestion** and choose **Set up**.
2. Go to the **Test event** step. Use the key you just created, or paste the one your service uses, and choose **Send test event**.
3. 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-test` so you can filter it out.
4. 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`.
