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

# Cost Tracking

> Automatic cost calculation, custom pricing JSON, model matching, and cost attribution by user/team/feature

## Overview

Anyway automatically tracks the cost of every LLM API call, helping you:

* Monitor spending in real-time
* Attribute costs to users, features, or teams
* Identify expensive operations
* Optimize token usage

## Automatic Cost Calculation

Both the Python and JavaScript SDKs automatically calculate costs based on bundled pricing data. Costs are added as span attributes:

* `gen_ai.usage.input_cost` — Input token cost (USD)
* `gen_ai.usage.output_cost` — Output token cost (USD)
* `gen_ai.usage.cost` — Total cost (USD)

### Supported Models

Pricing is included for 150+ models from these providers:

| Provider      | Example Models                                              |
| ------------- | ----------------------------------------------------------- |
| **OpenAI**    | gpt-4o, gpt-4o-mini, gpt-4-turbo, o1, o1-mini               |
| **Anthropic** | claude-sonnet-4-20250514, claude-3.5-sonnet, claude-3-haiku |
| **Google**    | gemini-1.5-pro, gemini-1.5-flash, gemini-2.0-flash          |
| **Meta**      | llama-3.1-405b, llama-3.1-70b, llama-3.1-8b                 |
| **Mistral**   | mistral-large, mistral-medium, mistral-small                |

<Note>
  The SDK ships with a default pricing file that is updated with each release. You can also provide custom pricing — see below.
</Note>

## Custom Pricing

Provide your own pricing data to add models or override prices:

<Tabs>
  <Tab title="Python">
    ```python theme={null}
    from anyway.sdk import Traceloop

    Traceloop.init(
        app_name="my-app",
        pricing_json_path="./pricing.json",
    )
    ```
  </Tab>

  <Tab title="JavaScript">
    ```typescript theme={null}
    import { initialize } from "@anyway-sh/node-server-sdk";

    initialize({
      appName: "my-app",
      pricingJsonPath: "./pricing.json",
    });
    ```
  </Tab>
</Tabs>

To disable cost calculation entirely:

<Tabs>
  <Tab title="Python">
    ```python theme={null}
    Traceloop.init(
        app_name="my-app",
        pricing_enabled=False,
    )
    ```
  </Tab>

  <Tab title="JavaScript">
    ```typescript theme={null}
    initialize({
      appName: "my-app",
      pricingEnabled: false,
    });
    ```
  </Tab>
</Tabs>

### Pricing JSON Format

```json theme={null}
{
  "chat": {
    "gpt-4o-mini": {
      "promptPrice": 0.00015,
      "completionPrice": 0.0006
    },
    "custom-model": {
      "promptPrice": 0.001,
      "completionPrice": 0.002
    }
  }
}
```

* `promptPrice`: Cost per 1K input tokens (USD)
* `completionPrice`: Cost per 1K output tokens (USD)

### Model Matching

The SDK matches model names using a three-tier strategy:

1. **Exact match** — `gpt-4o-mini` matches `gpt-4o-mini`
2. **Date suffix stripped** — `gpt-4o-2024-08-06` matches `gpt-4o`
3. **Longest prefix** — `gpt-4o-mini-custom` matches `gpt-4o-mini`

Unknown models are silently skipped — no error, just no cost attributes on that span.

## Cost Attribution

Use association properties to track costs by user, feature, or team:

<Tabs>
  <Tab title="Python">
    ```python theme={null}
    from anyway.sdk.decorators import workflow, task

    @workflow(name="user-query", association_properties={
        "user_id": "user-123",
        "team": "growth",
        "feature": "chatbot",
    })
    def handle_query(query: str):
        return call_llm(query)
    ```
  </Tab>

  <Tab title="JavaScript">
    ```typescript theme={null}
    await withWorkflow(
      {
        name: "user-query",
        associationProperties: {
          userId: "user-123",
          team: "growth",
          feature: "chatbot",
        },
      },
      async () => {
        return callLlm(query);
      },
    );
    ```
  </Tab>
</Tabs>

Then filter costs in the dashboard by these attributes.

## Cost Dashboard

The Cost Dashboard shows:

* **Total spend** — Current period and trend
* **Cost by model** — Which models cost the most
* **Cost by association** — Filter by user, team, or feature
* **Daily trends** — Spending over time

## Cost Optimization Tips

<AccordionGroup>
  <Accordion title="Use cheaper models for simple tasks" icon="lightbulb">
    Not every task needs a large model. Use `gpt-4o-mini` or `claude-3-haiku` for:

    * Classification tasks
    * Simple extractions
    * Formatting/transformation
  </Accordion>

  <Accordion title="Optimize prompts" icon="lightbulb">
    Shorter prompts = lower costs:

    * Remove redundant instructions
    * Use examples efficiently
    * Consider prompt caching
  </Accordion>

  <Accordion title="Set token limits" icon="lightbulb">
    Prevent runaway costs with `max_tokens`:

    ```python theme={null}
    response = client.chat.completions.create(
        model="gpt-4o",
        max_tokens=500,
        messages=[...]
    )
    ```
  </Accordion>
</AccordionGroup>

## Next Steps

<CardGroup cols={2}>
  <Card title="Usage Limits" icon="gauge" href="/billing/usage-limits">
    Set usage quotas
  </Card>

  <Card title="Payments" icon="credit-card" href="/features/payments">
    Create payment links and accept payments
  </Card>
</CardGroup>
