---
title: Request tagging
description: Add custom tags to model requests for usage tracking, cost attribution, and filtering in Unity Gateway.
canonical_url: https://docs.databricks.com/aws/en/ai-gateway/request-tagging
---

# Request tagging

Attach custom key-value tags to individual requests using the `Databricks-Ai-Gateway-Request-Tags` HTTP header. Unity Gateway logs request tags to the `request_tags` column in both the [usage tracking](https://docs.databricks.com/aws/en/ai-gateway/usage-tracking.md) system table and [inference tables](https://docs.databricks.com/aws/en/ai-gateway/inference-tables.md), enabling you to track costs, attribute usage, and filter analytics by project, team, environment, or another dimension.

On requests where service policies run, custom policies can also evaluate these tags as `event:context.request_tags`. External service policies receive them as `event.context.request_tags`, including in Log mode. For example, a policy can require a tag to be present before allowing a request. See [Request tags in service policies](https://docs.databricks.com/aws/en/data-governance/unity-catalog/service-policies/policy-function-reference.md#request-tags) and the [external service policy API reference](https://docs.databricks.com/aws/en/data-governance/unity-catalog/service-policies/external-policy-api.md#event).

The header value must be a JSON object mapping string keys to string values. For example:

```json
{ "project": "chatbot", "team": "ml-platform", "environment": "production" }
```

## Tag a model service request

Use the `extra_headers` parameter (Python) or pass the header directly (REST API) to attach tags to a model service request:

### Python (OpenAI SDK)

```python
from openai import OpenAI
import json
import os

DATABRICKS_TOKEN = os.environ.get('DATABRICKS_TOKEN')

client = OpenAI(
  api_key=DATABRICKS_TOKEN,
  base_url="https://<workspace-url>/ai-gateway/mlflow/v1"
)

request_tags = {"project": "chatbot", "team": "ml-platform"}

chat_completion = client.chat.completions.create(
  messages=[
    {"role": "user", "content": "What is Databricks?"},
  ],
  model="<model-service>",
  max_tokens=256,
  extra_headers={
    "Databricks-Ai-Gateway-Request-Tags": json.dumps(request_tags)
  }
)
```

### Python (Anthropic SDK)

```python
import anthropic
import json
import os

DATABRICKS_TOKEN = os.environ.get('DATABRICKS_TOKEN')

request_tags = {"project": "chatbot", "team": "ml-platform"}

client = anthropic.Anthropic(
  api_key="unused",
  base_url="https://<workspace-url>/ai-gateway/anthropic",
  default_headers={
    "Authorization": f"Bearer {DATABRICKS_TOKEN}",
    "Databricks-Ai-Gateway-Request-Tags": json.dumps(request_tags),
  },
)

message = client.messages.create(
  model="<model-service>",
  max_tokens=256,
  messages=[
    {"role": "user", "content": "What is Databricks?"},
  ],
)
```

### REST API

```bash
curl \
  -u token:$DATABRICKS_TOKEN \
  -X POST \
  -H "Content-Type: application/json" \
  -H 'Databricks-Ai-Gateway-Request-Tags: {"project": "chatbot", "team": "ml-platform"}' \
  -d '{
    "model": "<model-service>",
    "max_tokens": 256,
    "messages": [
      {"role": "user", "content": "What is Databricks?"}
    ]
  }' \
  https://<workspace-url>/ai-gateway/mlflow/v1/chat/completions
```

Replace `<workspace-url>` with your Databricks workspace URL and `<model-service>` with the fully qualified name of your model service.

## Tag a model provider service request

When you query a model provider service directly, send the tags header alongside the `Databricks-Model-Provider-Service` header:

### Python

```python
from openai import OpenAI
import json

client = OpenAI(
    api_key="<databricks-token>",
    base_url="https://<workspace-url>/ai-gateway/openai/v1",
    default_headers={"Databricks-Model-Provider-Service": "main.default.openai_prod"},
)

request_tags = {"project": "chatbot", "team": "ml-platform"}

response = client.chat.completions.create(
    model="gpt-5.5",
    messages=[{"role": "user", "content": "What is Databricks?"}],
    extra_headers={"Databricks-Ai-Gateway-Request-Tags": json.dumps(request_tags)},
)
```

### REST

```bash
curl https://<workspace-url>/ai-gateway/openai/v1/chat/completions \
  -H "Authorization: Bearer $DATABRICKS_TOKEN" \
  -H "Databricks-Model-Provider-Service: main.default.openai_prod" \
  -H "Content-Type: application/json" \
  -H 'Databricks-Ai-Gateway-Request-Tags: {"project": "chatbot", "team": "ml-platform"}' \
  -d '{
    "model": "gpt-5.5",
    "messages": [{"role": "user", "content": "What is Databricks?"}]
  }'
```
