> ## Documentation Index
> Fetch the complete documentation index at: https://docs.getunbound.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# LiteLLM

> Apply Unbound policies to the traffic passing through your LiteLLM proxy

## What is the LiteLLM integration?

[LiteLLM](https://docs.litellm.ai) is an open-source proxy that puts one endpoint in front of every model provider. Its built-in connector, `generic_guardrail_api`, sends each call to Unbound.

Unbound checks every call twice — the prompt before the model, the reply after it — against your [Security policies](https://docs.getunbound.ai/policies/security-policies.md) and [Tool policies](https://docs.getunbound.ai/policies/tool-policies.md). Calls appear under [Logs](https://docs.getunbound.ai/dashboard/logs).

## Prerequisites

* **LiteLLM** 1.106.2.
* **Admin access** to Unbound.

## Get your API key

1. Open **Settings → API Tokens** in Unbound.
2. **Generate Token** with **Full Access**, and copy it; it is shown once.

The token expires after at most 90 days. Generate a new one before then and update the proxy, or set `fail_on_error: true` below so an expired token stops traffic instead of letting it through unchecked.

Set it in your proxy's environment:

```bash theme={null}
export UNBOUND_API_KEY=<your-api-key>
export GENERIC_LOGGER_HEADERS="x-api-key=$UNBOUND_API_KEY"
```

## Setup

Add to your LiteLLM `config.yaml`:

```yaml theme={null}
guardrails:
  - guardrail_name: unbound
    litellm_params:
      guardrail: generic_guardrail_api
      mode: [pre_call, post_call]
      api_base: https://api.getunbound.ai/v1/app/analyze
      api_key: os.environ/UNBOUND_API_KEY
      default_on: true
      unreachable_fallback: fail_open
      fail_on_error: false
      streaming_buffer_until_moderated: true

litellm_settings:
  success_callback: ["generic_api"]
environment_variables:
  GENERIC_LOGGER_ENDPOINT: "https://api.getunbound.ai/v1/app/analyze/beta/litellm_basic_guardrail_api"
  DEFAULT_BATCH_SIZE: "5"
```

Restart the proxy.

| Setting | What it does |
| - | - |
| `mode` | `pre_call` checks the prompt, `post_call` checks the reply. Keep both — tool calls are in the reply |
| `api_base` | LiteLLM appends `/beta/litellm_basic_guardrail_api`. Keep the `/v1/app/analyze` path |
| `default_on` | Checks every model in `model_list` |
| `unreachable_fallback`, `fail_on_error` | Let calls through when Unbound cannot answer |
| `streaming_buffer_until_moderated` | **Coming soon.** Holds a streamed reply until the check finishes |
| `success_callback: ["generic_api"]` | Sends each call's real token usage, so Logs show exact tokens and cost |
| `GENERIC_LOGGER_ENDPOINT` | Where the usage goes. Same endpoint as the check |
| `GENERIC_LOGGER_HEADERS` | Your Unbound API key, sent with the usage |
| `DEFAULT_BATCH_SIZE` | Calls per usage post. Keeps each post under Unbound's size limit |

> [!NOTE]
>
> `guardrails` must be at the top level of `config.yaml`. Under `litellm_settings` it is ignored.

> [!WARNING]
>
> On LiteLLM 1.106.2, a coding agent can run a blocked command before the block arrives. The block is still recorded in Logs.
>
> **Coming soon:** holding streamed replies until the check finishes.

## Self-hosted models and cost

Unbound prices each call from its model catalog. A self-hosted model, or a model your users reach through an alias such as `coding-model`, is not in that catalog, so Unbound bills its calls at a default rate.

To bill your own rate, price these models in LiteLLM. Add `model_info` to each entry in `model_list`:

```yaml theme={null}
model_list:
  - model_name: coding-model                  # the name your users call
    litellm_params:
      model: hosted_vllm/Qwen/Qwen3-32B
      api_base: http://vllm.internal:8000/v1
    model_info:
      input_cost_per_token: 0.0000002         # USD per input token
      output_cost_per_token: 0.0000008        # USD per output token
```

| Field | What it does |
| - | - |
| `input_cost_per_token`, `output_cost_per_token` | LiteLLM works out each call's cost and sends it as `response_cost` with the usage |

For a model you host yourself, pick the rate you want to report. Your own GPU cost per token works, and so does a hosted provider's list price for the same model, which lets you compare it with your other providers.

## Verification

1. Send a request. It appears under [Logs](https://docs.getunbound.ai/dashboard/logs) with its prompt and response.
2. Send a prompt with something your Security policy detects, such as an API key. The detection is recorded.
3. Ask a coding agent to run a command your Tool policies block, such as `printenv`. The agent reports an error.
4. A few seconds after a call, its tokens appear in Logs. If they stay at 0, check `GENERIC_LOGGER_ENDPOINT` and `GENERIC_LOGGER_HEADERS`.

If nothing reaches Logs, check that `UNBOUND_API_KEY` is set and `api_base` ends in `/v1/app/analyze`.

## Cost for self-hosted models

Each call's cost shows up in [Cost Management](https://docs.getunbound.ai/dashboard/cost-management). Models LiteLLM already has a price for need nothing extra. For a self-hosted model, give LiteLLM its price per token under `model_info`:

```yaml theme={null}
model_list:
  - model_name: coding-model
    litellm_params:
      model: openai/qwen-coder
      api_base: http://10.0.0.5:8000/v1
    model_info:
      input_cost_per_token: 0.0000005   # $0.50 per 1M input tokens
      output_cost_per_token: 0.000002   # $2.00 per 1M output tokens
```

Restart the proxy. LiteLLM works out the cost of each call and sends it with the usage, so the `success_callback` and `GENERIC_LOGGER_*` settings from [Setup](#setup) need to be in place.

A self-hosted model with no price shows a cost of \$0. Its tokens are still counted.

## Attribution

Each call is attributed to the owner of the [virtual key](https://docs.litellm.ai/docs/proxy/virtual_keys) that made it, using the key's `user_email`. Calls on a shared master key are attributed to the owner of the Unbound API key. For per-user policies, give each person a virtual key with `user_email`.

> [card] [Security Policies](https://docs.getunbound.ai/policies/security-policies.md)
>
> Detect and redact secrets and personal data in prompts

> [card] [Tool Policies](https://docs.getunbound.ai/policies/tool-policies.md)
>
> Audit, warn or block risky commands and MCP calls


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.