AI › AI Tools

Reading token usage and cost in LiteLLM logs

Look up tokens and cost for a LiteLLM request, then compare a local model run with a mock reply using explicit price settings.

Reading token usage and cost in LiteLLM logs

The summary came back with token counts, but the response had no cost header. I matched its response ID to a row in LiteLLM’s spend logs, then compared it with a mock request that used explicit token prices.

I ran LiteLLM Proxy v1.111.1 with PostgreSQL 17, Ollama 0.40.18, curl, and jq. Ollama generated the first response locally. A local fixture supplied fixed token counts for the second request so I could check how LiteLLM calculated the price. LiteLLM release

Start with the usage in the response

I reused the gateway and Jekyll 4.4.0 release-note excerpt from the virtual key exercise. Each request travels from the app through LiteLLM to an upstream. The app can read the token counts from usage in the response, and the proxy records request details in its spend log. I set turn_off_message_logging so the log could retain the metadata for this exercise without storing the prompt and generated text. Spend tracking · Request and response logging options

What to checkWhere to find it
Input and output tokens for the completed requestThe response body’s usage
The cost calculated by the proxyx-litellm-response-cost when present, and the log’s spend field
Which app key and model the proxy recordedmetadata.user_api_key_alias and model_group in the log

Send two requests through the same gateway

I gave release-summary no custom prices and routed it to smollm2:135m-instruct-q4_K_M on Ollama. I pointed release-priced-check at a local OpenAI-compatible fixture. The fixture returns usage for 80 input tokens and 20 output tokens. I added the rates below under the fixture deployment’s model_info. Each rate is in US dollars per token. These settings let me work out the expected spend before making a request. Custom LLM pricing

1
2
3
model_info:
  input_cost_per_token: 0.000002
  output_cost_per_token: 0.000006

In Bash, set GATEWAY to the proxy address and APP_KEY to a virtual key that allows both aliases. Set MODEL to the alias to call and save the excerpt as release-notes.md. I ran the same command for each alias and kept the response body and headers for both requests. The headers let me check the cost reported to the client before looking up the stored row.

1
2
3
4
5
6
7
8
9
10
11
12
13
jq -n --arg model "$MODEL" --rawfile notes release-notes.md '{
  model: $model,
  messages: [
    {role: "system", content: "Summarize the release notes in one short sentence."},
    {role: "user", content: $notes}
  ],
  max_tokens: 100, stream: false
}' |
  curl -fsS "$GATEWAY/v1/chat/completions" \
    -H "Authorization: Bearer ${APP_KEY:?}" -H 'Content-Type: application/json' \
    --data-binary @- -D response.headers -o response.json

jq '{id, model, usage}' response.json

Use the response ID to find the matching spend record

For both Chat Completions calls in this version, the JSON response’s id matched the spend log’s request_id. The response also included x-litellm-call-id, with a different value. Passing that header value to the same request_id filter returned no record for either call. I kept the IDs through the test and checked the match directly. A different endpoint or version may expose a different correlation field, so check a real response and row when adapting the example. LiteLLM response headers · Spend log endpoints

I used the local administrator key to query the logs. Set LOG_DAY to the UTC day that contains the request. If the row is missing immediately after the response, give the proxy a moment to record it and repeat the same query. The app sends requests with its virtual key and the operator uses the administrator key for this lookup.

1
2
3
4
5
6
7
8
9
REQUEST_ID=$(jq -er '.id' response.json)
curl -fsSG "$GATEWAY/spend/logs/v2" \
  -H "Authorization: Bearer ${LITELLM_MASTER_KEY:?}" \
  --data-urlencode "request_id=$REQUEST_ID" \
  --data-urlencode "start_date=$LOG_DAY 00:00:00" \
  --data-urlencode "end_date=$LOG_DAY 23:59:59" |
  jq '.data[] | {request_id, model_group, prompt_tokens,
                completion_tokens, spend,
                key_alias: .metadata.user_api_key_alias}'

Read the tokens and cost from the two rows

UpstreamInput / output tokensResponse cost headerStored spend
Real local Ollama generation84 / 22AbsentUSD 0
Mock response with fixed usage80 / 200.00028USD 0.00028

The fixture’s prices give 80 × 0.000002 + 20 × 0.000006 = 0.00028. Both the header and the stored row showed that value. Ollama returned 106 total tokens and the proxy recorded zero spend in this setup. That zero is the value tracked by the proxy for the request. The laptop’s resources are a separate operating cost.

LiteLLM execution output matching the response ID to spend logs for an Ollama request and a priced mock reply Figure 1. I shortened the IDs in the captured output. Both response body IDs match their spend log request IDs.

Check the pricing data when a charge looks wrong

Start with the usage in the body. If the token counts are present and the cost header is missing, look up the request in the spend log. For a paid upstream, check the proxy’s pricing entry for the deployment and reconcile it with the provider’s usage. In the direct OpenRouter example, I read the provider’s own response and request log for the same reason.

  • Confirm the units on the prices. A rate per token and a rate per million tokens belong in different fields or calculations.
  • Check the UTC date range and the actual ID before concluding a record is missing.
  • Record the price settings beside any results from local or mock calls so a reader can reproduce the calculation.
This post is licensed under CC BY 4.0 by the author.