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

# Cost Tracking

> Automatic token counting and cost calculation for LLM calls

When you use TraceRoot's auto-instrumentation for OpenAI, Anthropic, or LangChain, token usage and cost are tracked automatically — no extra code needed.

<Tabs>
  <Tab title="Python">
    ```python theme={null}
    import traceroot
    from traceroot import Integration

    traceroot.initialize(integrations=[Integration.OPENAI])

    # All OpenAI calls are now automatically tracked — tokens, cost, model
    client.chat.completions.create(...)
    ```
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={null}
    import OpenAI from 'openai';
    import { TraceRoot } from '@traceroot-ai/traceroot';

    TraceRoot.initialize({ instrumentModules: { openAI: OpenAI } });

    // All OpenAI calls are now automatically tracked — tokens, cost, model
    await client.chat.completions.create({ ... });
    ```
  </Tab>
</Tabs>

## How It Works

1. **Auto-instrumentation** — TraceRoot intercepts LLM calls from OpenAI, Anthropic, and LangChain and captures token usage from the API response automatically.
2. **Fallback estimation** — When token counts aren't in the response, TraceRoot estimates using tiktoken.
3. **Cost calculation** — Token counts are multiplied by the model's pricing from TraceRoot's pricing table.

## What Gets Tracked

Each LLM span captures:

| Metric        | Description                                   |
| ------------- | --------------------------------------------- |
| Input tokens  | Tokens in the prompt/messages                 |
| Output tokens | Tokens in the completion/response             |
| Total tokens  | Sum of input and output                       |
| Cost (USD)    | Calculated from token usage and model pricing |

## Viewing Costs

In the dashboard, costs are visible at both the span and trace level:

* **Per span** — see the cost of each individual LLM call
* **Per trace** — see the total cost aggregated across all LLM calls in the trace

## Manual Token Reporting

For custom or unsupported providers, report token usage manually:

<Tabs>
  <Tab title="Python">
    ```python theme={null}
    from traceroot import observe, update_current_span

    @observe(name="custom_llm", type="llm")
    def call_custom_llm(prompt: str):
        response = my_llm.generate(prompt)

        update_current_span(
            model="my-custom-model",
            usage={
                "input_tokens": response.input_token_count,
                "output_tokens": response.output_token_count,
            },
        )

        return response.text
    ```
  </Tab>

  <Tab title="TypeScript">
    ```typescript theme={null}
    import { observe, updateCurrentSpan } from '@traceroot-ai/traceroot';

    const result = await observe({ name: 'custom_llm', type: 'llm' }, async () => {
      const response = await myLlm.generate(prompt);

      updateCurrentSpan({
        metadata: {
          model: 'my-custom-model',
          usage: {
            input_tokens: response.inputTokenCount,
            output_tokens: response.outputTokenCount,
          },
        },
      });

      return response.text;
    });
    ```
  </Tab>
</Tabs>
