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

# Cost by Environment

> Separate production and staging LLM spend so you can budget each independently.

**Tag requests with their environment so production and staging costs never bleed into each other in the dashboard.**

<Steps>
  <Step title="Set X-Toolken-Metadata-Environment on every request">
    Add `X-Toolken-Metadata-Environment` to your requests. The value is any string, but `production` and `staging` are the most useful conventions.

    <CodeGroup>
      ```python Python theme={null}
      from openai import OpenAI

      client = OpenAI(
          base_url="https://gateway.toolken.ai/v1",
          api_key="sk-...",  # your provider key, forwarded untouched
          default_headers={
              "X-Toolken-Key": "tk_live_...",
              "X-Toolken-Metadata-Environment": "production",
          },
      )
      client.chat.completions.create(
          model="gpt-4o-mini",
          messages=[{"role": "user", "content": "Draft a release note for version 3.0."}],
      )
      ```

      ```javascript Node theme={null}
      import OpenAI from "openai";

      const client = new OpenAI({
        baseURL: "https://gateway.toolken.ai/v1",
        apiKey: process.env.OPENAI_API_KEY, // forwarded untouched
        defaultHeaders: {
          "X-Toolken-Key": "tk_live_...",
          "X-Toolken-Metadata-Environment": process.env.NODE_ENV === "production" ? "production" : "staging",
        },
      });
      await client.chat.completions.create({
        model: "gpt-4o-mini",
        messages: [{ role: "user", content: "Draft a release note for version 3.0." }],
      });
      ```

      ```bash curl theme={null}
      curl https://gateway.toolken.ai/v1/chat/completions \
        -H "Authorization: Bearer $OPENAI_API_KEY" \
        -H "X-Toolken-Key: tk_live_..." \
        -H "X-Toolken-Metadata-Environment: production" \
        -H "Content-Type: application/json" \
        -d '{"model":"gpt-4o-mini","messages":[{"role":"user","content":"Draft a release note for version 3.0."}]}'
      ```
    </CodeGroup>

    <Tip>Drive the value from an environment variable (like `NODE_ENV` or `RAILS_ENV`) so you never have to change application code between deploys.</Tip>
  </Step>

  <Step title="Open the dashboard and filter or group by Environment">
    Go to the cost view. Use the filter to show only `production` or only `staging`. Or group by **Environment** to see both side by side with their totals.
  </Step>
</Steps>

<AccordionGroup>
  <Accordion title="Combine environment with feature">
    Layer `X-Toolken-Metadata-Feature` and `X-Toolken-Metadata-Environment` to get a full picture: how much did each agent cost, split by environment?

    ```python theme={null}
    default_headers={
        "X-Toolken-Key": "tk_live_...",
        "X-Toolken-Metadata-Feature": "report-generator",
        "X-Toolken-Metadata-Environment": "production",
    }
    ```

    Group by **Feature** and filter to `production` to see production-only cost per agent.
  </Accordion>

  <Accordion title="Track per session">
    Add `X-Toolken-Metadata-Session-Id` to group all the LLM calls in a single user session. Combine with environment to separate session costs by deployment:

    ```python theme={null}
    default_headers={
        "X-Toolken-Key": "tk_live_...",
        "X-Toolken-Metadata-Environment": "production",
        "X-Toolken-Metadata-Session-Id": session_id,
    }
    ```

    The `X-Toolken-Metadata-*` convention maps any header suffix to a metadata key. `X-Toolken-Metadata-Session-Id` becomes the `session_id` dimension in the dashboard.
  </Accordion>
</AccordionGroup>

## Next

<CardGroup cols={2}>
  <Card title="Per-Customer Cost" icon="user-check" href="/use-cases/per-customer-cost">Attribute spend to individual end-customers.</Card>
  <Card title="Compare Providers by Cost" icon="scale-balanced" href="/use-cases/compare-providers-by-cost">See what the same workload costs across providers.</Card>
</CardGroup>
