Log LiteLLM Gateway requests to Zerobus Ingest
For the callback's full reference, see Databricks Zerobus in the LiteLLM documentation.
Maintained by: the LiteLLM project, in the BerriAI/litellm repository.
The LiteLLM Gateway (the LiteLLM proxy server) has a built-in zerobus logging callback. It writes one row for each request that reaches LiteLLM's logging callbacks, successful or failed, with the model, status, token counts, cost, caller, and timing, to a Delta table through Zerobus Ingest. The callback gets and refreshes its own OAuth token from a service principal, so you don't write any code. You don't need to run a message bus between the gateway and the lakehouse.
When to use this integration
- You run the LiteLLM Gateway in front of your models and want request logs, cost, and usage in Unity Catalog, next to the rest of your data.
- You want one table for every model provider behind the gateway, with the prompt and response stored as
VARIANTor redacted.
How it works
The callback queues a row for each finished request, both successes and failures. It sends the queue as a JSON batch to the Zerobus Ingest REST insert endpoint every flush_interval seconds, or sooner when the queue reaches batch_size rows. It requests its token from <workspace-url>/oidc/v1/token with the client credentials grant, the all-apis scope, and the authorization_details that scope the token to USE CATALOG and USE SCHEMA on the table's catalog and schema, and to SELECT and MODIFY on the table. Logging runs separately from the model call, so a logging error doesn't change the response that the client gets.
The callback keeps its queue in memory. A process that stops, a batch that Zerobus Ingest rejects, or a full queue can lose rows. A retried batch can write a row more than one time. See Delivery guarantees. To count requests, count the distinct litellm_call_id values. A gateway retry or fallback can write more than one row with the same litellm_call_id, so don't deduplicate on it when you need each attempt's status, usage, or timing.
Requirements
- A workspace in a region that supports Zerobus Ingest. See Availability.
- A service principal with an OAuth secret that has
all-apisaccess, which is the default. LiteLLM requestsall-apisfor every token, so a secret with narrower scopes can't authenticate the callback. Scopes can't change after a secret is created, so generate a new secret if the existing one is narrower. See Configure authentication and Scoped OAuth secrets. - A target table that meets the Table requirements. You create it in Create the target table and grant access.
- LiteLLM 1.105.0 and above, except 1.107.0, with at least one working model in the gateway. The
zerobuscallback ships in LiteLLM 1.105.0 and above. LiteLLM 1.107.0 was a compromised release, so don't install it. See the LiteLLM security incident report. - Network access from the gateway host to your Zerobus Ingest endpoint and your workspace URL on port
443.
Create the target table and grant access
The callback writes a fixed set of columns, and LiteLLM generates the matching CREATE TABLE statement. Generate it with the same LiteLLM version that the gateway runs, so the statement matches the columns that version writes. Replace <catalog>.<schema>.<table> with your table name:
python -c "from litellm.integrations.zerobus.row import create_table_sql; print(create_table_sql('<catalog>.<schema>.<table>'))"
For LiteLLM 1.109.1, the command prints the following statement. Run the statement that your version prints in Databricks SQL:
CREATE TABLE <catalog>.<schema>.<table> (
id STRING,
trace_id STRING,
session_id STRING,
litellm_call_id STRING,
call_type STRING,
status STRING,
model STRING,
model_group STRING,
model_id STRING,
custom_llm_provider STRING,
api_base STRING,
stream BOOLEAN,
cache_hit BOOLEAN,
start_time TIMESTAMP,
end_time TIMESTAMP,
completion_start_time TIMESTAMP,
response_time DOUBLE,
prompt_tokens LONG,
completion_tokens LONG,
total_tokens LONG,
response_cost DOUBLE,
saved_cache_cost DOUBLE,
api_key_hash STRING,
api_key_alias STRING,
team_id STRING,
team_alias STRING,
user_id STRING,
org_id STRING,
end_user STRING,
requester_ip_address STRING,
user_agent STRING,
request_tags VARIANT,
messages VARIANT,
response VARIANT,
error_str STRING,
error_information VARIANT,
metadata VARIANT,
model_parameters VARIANT,
hidden_params VARIANT,
guardrail_information VARIANT,
cost_breakdown VARIANT
);
Nested fields, such as messages and response, are VARIANT columns. The callback sends them as JSON-encoded strings, and Zerobus Ingest writes them unshredded into the column. Keep the types that the statement generates.
Grant the service principal access. Replace <service-principal-uuid> with its client ID:
GRANT USE CATALOG ON CATALOG <catalog> TO `<service-principal-uuid>`;
GRANT USE SCHEMA ON SCHEMA <catalog>.<schema> TO `<service-principal-uuid>`;
GRANT MODIFY, SELECT ON TABLE <catalog>.<schema>.<table> TO `<service-principal-uuid>`;
Install the LiteLLM Gateway
Install LiteLLM 1.109.1 with the proxy extras:
pip install 'litellm[proxy]==1.109.1'
If the gateway already runs, check its version with pip show litellm. If it's lower than 1.105.0, upgrade to 1.109.1. If it's 1.107.0, treat the host as compromised. Upgrading doesn't remove the compromise. Follow the LiteLLM security incident report before you continue. It covers how to check for compromise, rebuilding the environment, and rotating the credentials that the host could reach.
Set the connection variables
Set these environment variables where the gateway runs:
Variable | Value |
|---|---|
| Your workspace URL, |
| The Zerobus Ingest endpoint for your workspace, including |
| The service principal's client ID. |
| The service principal's OAuth secret. |
| The full table name that you created, |
To find your workspace URL and endpoint, see Get your workspace URL and Zerobus Ingest endpoint. The endpoint has this format:
https://<workspace-id>.zerobus.<region>.gcp.databricks.com
Turn on the callback
Add the zerobus callback to the litellm_settings section of the gateway's config.yaml. If callbacks already lists other callbacks, add 'zerobus' to that list instead of replacing it, so the existing callbacks keep running. The os.environ/ values read the variables that you set in Set the connection variables:
litellm_settings:
callbacks: ['zerobus']
zerobus_params:
workspace_url: os.environ/ZEROBUS_WORKSPACE_URL
server_endpoint: os.environ/ZEROBUS_SERVER_ENDPOINT
client_id: os.environ/ZEROBUS_CLIENT_ID
client_secret: os.environ/ZEROBUS_CLIENT_SECRET
table_name: os.environ/ZEROBUS_TABLE_NAME
batch_size: 100
flush_interval: 5
turn_off_message_logging: false
Set these optional settings under zerobus_params, as in the YAML example:
Setting | Default | Description |
|---|---|---|
|
| The queue size that triggers an early send. It isn't a maximum batch size. |
|
| The seconds between sends. The example uses |
|
| Set to |
Keep the callback in config.yaml to log failed requests too. A callback that you add in the LiteLLM Admin UI logs successful requests only.
Start the gateway and send a request
Start the gateway with your configuration file:
litellm --config config.yaml
Send a request through the gateway. Replace <model-name> with a model from your model_list, and <litellm-key> with a key that can call it:
curl http://localhost:4000/v1/chat/completions \
-H 'Authorization: Bearer <litellm-key>' \
-H 'Content-Type: application/json' \
-d '{"model": "<model-name>", "messages": [{"role": "user", "content": "Roll a die"}]}'
Check the table
After one flush interval, query the table:
SELECT start_time, model_group, status, total_tokens, response_cost, error_str
FROM <catalog>.<schema>.<table>
ORDER BY start_time DESC
LIMIT 10;
Each request that reaches LiteLLM's logging callbacks adds one row. A failed request has status set to failure and the error in error_str. A request that the gateway rejects before the logging callbacks run, for example in a guardrail, might not add a row.
Troubleshooting
The gateway logs callback errors, and they don't change the client's response.
Symptom | Cause | Fix |
|---|---|---|
The gateway log shows | The token request names the table from | Check that |
The gateway log shows | A connection variable is missing or malformed. | Check each value against the table in Set the connection variables. |
Inserts fail with HTTP 400 and an empty body | The workspace ID in | Copy both values from Get your workspace URL and Zerobus Ingest endpoint. |
Inserts fail with a decoder or encoder error | The table doesn't match the columns that the gateway's LiteLLM version writes, for example after an upgrade. | Generate the statement again with the gateway's version, as in Create the target table and grant access, and update the table to match it. |
No rows appear | The callback sends rows every | Wait one flush interval, and then check the log of each replica for callback errors. |
Additional resources
-
- Write a client
- Learn how to write a Zerobus Ingest client, including the REST insert endpoint and the OAuth token request.
-
- Zerobus Ingest error handling
- Look up the error codes that Zerobus Ingest returns and how to handle them.