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

# App Attribution

Track, measure, and allocate your API usage across apps, projects, and customers. App Attribution lets you tag every API call with metadata so you can see exactly where your budget is going.

## What's in It for You

* **Clear usage insights** — See how each app, project, or customer uses your API
* **Cost allocation** — No more guesswork when dividing bills
* **Budget controls** — Set spending limits per tag and get alerts
* **Data-driven decisions** — Spot trends and optimize your spend

## How It Works

AnyAPI uses **tags** and **metadata** to attribute API requests. Pass them in the `metadata` field of your request body, and every request gets tracked and grouped accordingly.

## Tagging Your Requests

### Using Tags

Tags are labels you attach to requests to track spending by category. Pass them as an array in the `metadata` field:

<CodeGroup>
  ```bash cURL theme={"system"}
  curl -X POST "https://api.anyapi.ai/v1/chat/completions" \
    -H "Authorization: Bearer your_api_key" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "openai/gpt-5",
      "messages": [
        {"role": "user", "content": "Hello, how can you help me?"}
      ],
      "metadata": {
        "tags": ["app:customer-support-bot", "env:production", "team:support"]
      }
    }'
  ```

  ```python Python theme={"system"}
  import requests

  response = requests.post(
      "https://api.anyapi.ai/v1/chat/completions",
      headers={
          "Authorization": "Bearer your_api_key",
          "Content-Type": "application/json"
      },
      json={
          "model": "openai/gpt-5",
          "messages": [
              {"role": "user", "content": "Hello, how can you help me?"}
          ],
          "metadata": {
              "tags": ["app:customer-support-bot", "env:production", "team:support"]
          }
      }
  )

  print(response.json())
  ```

  ```javascript JavaScript theme={"system"}
  const response = await fetch('https://api.anyapi.ai/v1/chat/completions', {
    method: 'POST',
    headers: {
      'Authorization': 'Bearer your_api_key',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      model: 'openai/gpt-5',
      messages: [
        { role: 'user', content: 'Hello, how can you help me?' }
      ],
      metadata: {
        tags: ['app:customer-support-bot', 'env:production', 'team:support']
      }
    })
  });

  const data = await response.json();
  console.log(data);
  ```
</CodeGroup>

### Using Custom Metadata

For richer tracking, pass `spend_logs_metadata` with custom key-value pairs that get logged to spend records:

```python Python theme={"system"}
response = requests.post(
    "https://api.anyapi.ai/v1/chat/completions",
    headers={
        "Authorization": "Bearer your_api_key",
        "Content-Type": "application/json"
    },
    json={
        "model": "anthropic/claude-sonnet-4",
        "messages": [
            {"role": "user", "content": "Analyze this quarterly data..."}
        ],
        "metadata": {
            "tags": ["app:bi-tool", "env:production"],
            "spend_logs_metadata": {
                "project": "q4-sales-analysis",
                "department": "marketing",
                "version": "1.5.2"
            }
        }
    }
)
```

### Tracking End Users

Use the `user` parameter to track spend per end user:

```python Python theme={"system"}
response = requests.post(
    "https://api.anyapi.ai/v1/chat/completions",
    headers={
        "Authorization": "Bearer your_api_key",
        "Content-Type": "application/json"
    },
    json={
        "model": "openai/gpt-5",
        "messages": [
            {"role": "user", "content": "How do I reset my password?"}
        ],
        "user": "customer_54321",
        "metadata": {
            "tags": ["app:support-bot"]
        }
    }
)
```

### Using Headers

You can also pass tags and metadata via HTTP headers:

```python Python theme={"system"}
headers = {
    "Authorization": "Bearer your_api_key",
    "Content-Type": "application/json",
    "x-litellm-tags": "app:my-chatbot,env:production",
    "x-litellm-spend-logs-metadata": '{"project": "q4-analysis", "version": "2.0"}'
}
```

## Tag Budgets

Set spending limits per tag to control costs. When a tag exceeds its budget, requests with that tag are blocked.

### Create a Tag Budget

```bash theme={"system"}
curl -X POST "https://api.anyapi.ai/tag/new" \
  -H "Authorization: Bearer your_api_key" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "app:customer-support-bot",
    "max_budget": 100.0,
    "budget_duration": "30d"
  }'
```

| Parameter         | Type   | Description                                    |
| ----------------- | ------ | ---------------------------------------------- |
| `name`            | string | Unique tag identifier                          |
| `max_budget`      | number | Spending limit in USD                          |
| `budget_duration` | string | Reset frequency: `"1d"`, `"7d"`, `"30d"`, etc. |

### Check Tag Info

```bash theme={"system"}
curl -X GET "https://api.anyapi.ai/tag/info?names=app:customer-support-bot" \
  -H "Authorization: Bearer your_api_key"
```

### Update a Tag Budget

```bash theme={"system"}
curl -X POST "https://api.anyapi.ai/tag/update" \
  -H "Authorization: Bearer your_api_key" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "app:customer-support-bot",
    "max_budget": 200.0
  }'
```

### Delete a Tag

```bash theme={"system"}
curl -X POST "https://api.anyapi.ai/tag/delete" \
  -H "Authorization: Bearer your_api_key" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "app:customer-support-bot"
  }'
```

## Tagging Strategies

### By Application

```python Python theme={"system"}
metadata = {
    "tags": ["app:customer-support-bot"]
}
```

### By Environment

```python Python theme={"system"}
metadata = {
    "tags": ["app:my-chatbot", "env:production"]
}
```

### Multi-Tenant SaaS

```python Python theme={"system"}
metadata = {
    "tags": ["app:saas-platform", "tenant:company-abc"],
    "spend_logs_metadata": {
        "tier": "enterprise"
    }
}
```

### By Team and Project

```python Python theme={"system"}
metadata = {
    "tags": ["team:marketing", "project:q4-campaign"]
}
```

## Best Practices

### Smart Tag Names

* **Be descriptive**: Use `app:customer-chat-prod` not `app1`
* **Stay consistent**: Pick a naming convention and stick to it (e.g., `key:value` format)
* **Include environment**: Separate `env:prod`, `env:staging`, `env:dev`

### Cost Management

* **Set tag budgets**: Start conservative, adjust as needed
* **Use budget durations**: Monthly (`30d`) resets for predictable billing
* **Tag everything**: Untagged requests are harder to attribute later

### Security

* **Hash user IDs**: Protect customer privacy when using the `user` parameter
* **No secrets in metadata**: Attribution data is logged in spend records

***

*Start with a simple tag on your next request, then gradually add more as your needs grow. Your future self (and your finance team) will thank you.*
