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

# Response cost

> Return the cost GoModel calculated for a request to the client that made it, as usage.cost in the response and in X-GoModel-Cost headers.

## Overview

GoModel can return the cost it calculated for a request to the client that made it. It is off by default. Turn it on when clients account for their own spend per request — for example an application that holds an inference-only key and attributes cost to its own users — so they need neither an admin key nor a later lookup in `/admin/usage/log`.

The shape follows OpenRouter's usage accounting: the cost is `usage.cost`, a JSON number in USD, in the usage object of the response. A client that already reads OpenRouter's cost works against GoModel unchanged.

Estimating costs, pricing, markups and the dashboard totals are covered in [Cost tracking](https://gomodel.enterpilot.io/docs/features/cost-tracking.md).

## Enable it

```yaml theme={null}
usage:
  response_cost_enabled: true # env: USAGE_RESPONSE_COST_ENABLED; default false
```

It needs usage tracking (`USAGE_ENABLED`, on by default): a request without a usage row has no cost to return.

## Read the cost

A buffered answer carries the cost in its `usage` object and in two response headers:

```bash theme={null}
curl -si http://localhost:8080/v1/chat/completions \
  -H "Authorization: Bearer $GOMODEL_MASTER_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model": "gpt-4o-mini", "messages": [{"role": "user", "content": "Say hello in one word."}]}'
```

```text theme={null}
HTTP/1.1 200 OK
X-GoModel-Cost: 0.00000315
X-GoModel-Cost-Currency: USD

{"id":"chatcmpl-...","object":"chat.completion","choices":[...],"usage":{"prompt_tokens":13,"completion_tokens":2,"total_tokens":15,"cost":0.00000315}}
```

The OpenAI SDKs keep response members they have no field for, so the cost is read from the extra fields of the usage object. On a stream it is on the last chunk that carries usage:

```python theme={null}
import os
from openai import OpenAI

client = OpenAI(
    base_url="http://localhost:8080/v1",
    api_key=os.environ["GOMODEL_MASTER_KEY"],
)

completion = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Say hello in one word."}],
)
print(completion.usage.model_extra.get("cost"))  # 3.15e-06; None when the cost is unknown

stream = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Say hello in one word."}],
    stream=True,
)
cost = None
for chunk in stream:
    if chunk.usage is not None:
        cost = chunk.usage.model_extra.get("cost")
print(cost)  # the cost of the whole request
```

The Anthropic Messages API and the Responses API carry it the same way; see *Buffered responses* and *Streamed responses* below for where it sits in each.

## Which cost

With [markups](https://gomodel.enterpilot.io/docs/features/cost-tracking.md#markups-and-billed-cost) enabled, the client gets the request's billed cost, and GoModel removes every cost the provider reported in a field it reads costs from (see *Upstream costs* below), so the response reveals neither the provider cost nor the markup. A cost a provider reports in a field GoModel does not know is not recognised as one and stays in the response. With markups off, it gets the provider cost (`total_cost`), also for a provider marked `billable: false`. Either way it is in USD and an estimate like every cost GoModel calculates, and it is the value on the request's usage row except for a cache hit without markups (see below).

**Format.** The amount is rounded to 12 decimal places and written as a plain decimal: no exponent, no float noise, no trailing zeros (`0.00003`, never `0.000030000000000000004`; `0.000000001`, never `1e-9`).

## Buffered responses

The cost is added at the end of the response's `usage` object:

```json Chat Completions theme={null}
{"id":"chatcmpl-1","object":"chat.completion","choices":[...],"usage":{"prompt_tokens":271,"completion_tokens":12,"total_tokens":283,"cost":0.00000542}}
```

```json Responses theme={null}
{"id":"resp_1","object":"response","status":"completed","output":[...],"usage":{"input_tokens":271,"output_tokens":12,"total_tokens":283,"cost":0.00000542}}
```

```json Anthropic Messages theme={null}
{"id":"msg_1","type":"message","content":[...],"stop_reason":"end_turn","usage":{"input_tokens":271,"output_tokens":12,"cost":0.00000542}}
```

```json Embeddings theme={null}
{"object":"list","data":[...],"model":"text-embedding-3-small","usage":{"prompt_tokens":8,"total_tokens":8,"cost":0.00000016}}
```

GoModel edits the body's bytes: only the usage object changes (with markups, the provider's cost fields listed under *Upstream costs* are removed too), and every other member keeps its order, escaping, formatting and precision. A buffered response also carries the same rounded value in two headers, the only channel for answers without a usage object (audio, most images):

```http theme={null}
X-GoModel-Cost: 0.00000542
X-GoModel-Cost-Currency: USD
```

## Streamed responses

A stream sends its headers before the cost is known, so it carries the cost in its events only: `usage.cost` is added to the usage object of each event that carries usage, normally the one with the final token counts. Each event is relayed as soon as it is complete, when its blank line arrives; only the event still arriving is held.

```text Chat Completions theme={null}
data: {"id":"chatcmpl-1","object":"chat.completion.chunk","choices":[],"usage":{"prompt_tokens":271,"completion_tokens":12,"total_tokens":283,"cost":0.00000542}}
```

```text Responses theme={null}
event: response.completed
data: {"type":"response.completed","response":{"id":"resp_1","status":"completed","usage":{"input_tokens":271,"output_tokens":12,"total_tokens":283,"cost":0.00000542}}}
```

```text Anthropic Messages theme={null}
event: message_delta
data: {"type":"message_delta","delta":{"stop_reason":"end_turn","stop_sequence":null},"usage":{"output_tokens":12,"cost":0.00000542}}
```

| Endpoint | Event | Member |
| - | - | - |
| `/v1/chat/completions` | the usage chunk | `usage.cost` |
| `/v1/responses` | `response.completed` or `response.incomplete` | `response.usage.cost` |
| `/v1/messages` | `message_delta` | `usage.cost` |

Every other event passes through unchanged, unless it holds a cost field that is removed (see *Upstream costs* below). A provider that reports usage on several chunks gets `usage.cost` on each of them that GoModel can price. The value is cumulative for the request so far, like the token counts in the same object: read it from the last event that carries usage, and never add them up. That value is the cost on the usage row. When the last usage event has no `usage.cost`, one on an earlier event covers the earlier token counts only (see *Coverage and limits*). A stream without usage carries no cost: GoModel asks providers for usage by default (`ENFORCE_RETURNING_USAGE_DATA`).

## Unknown is not zero

A request's cost is unknown when its model has no pricing and its provider reported no cost GoModel reads (OpenRouter, xAI and Eden AI report one, which prices a request even without model pricing). The response then carries no `usage.cost` and no header, in every mode. `0` always means a priced request that cost nothing.

| Request | Markups off | Markups on |
| - | - | - |
| Priced | `total_cost` | `billed_cost` |
| Cost unknown | none | none |
| `billable: false` provider, priced | `total_cost` | `0` |
| `billable: false` provider, cost unknown | none | none |
| Cache hit, priced | `0` | `billed_cost` with `bill_cache_hits`, `0` without |
| Cache hit, cost unknown | none | none |

**Response-cache hits** (`X-Cache: HIT`) call no provider, so their provider cost is `0`. Their usage row still records what the cached answer would have cost (`total_cost`), which is where the dashboard's savings come from; that is not what the client spent. The cache stores answers without the cost, so a hit carries its own.

## Upstream costs

An upstream may report its own `usage.cost`: OpenRouter does, and so does another GoModel. With the setting on, `usage.cost` belongs to this gateway, and an upstream's value is never presented as its own:

* Every upstream `cost` member is removed from every usage object GoModel reads, in any letter case, spelled with escapes, or repeated. The top-level `usage` and `response.usage` then end up with exactly one `cost`, this gateway's, or none when that is unknown, so a parser that keeps the first or the last duplicate reads the same value. `message.usage` ends up with none.
* GoModel reads three usage objects, in a buffered body and in a stream event alike: the top-level `usage`, `response.usage` and `message.usage`. The cost is added to `usage` and `response.usage`; `message.usage` (Anthropic's `message_start`) only loses members. A usage object nested anywhere else is not edited.
* With markups enabled, every field GoModel knows a provider reports its cost in is removed too, all duplicates and case variants included, so a reseller's customer learns neither the provider cost nor the markup:

  | Field | Where | Reported by |
  | - | - | - |
  | `cost_details` | usage object | OpenRouter (for example `upstream_inference_cost`) |
  | `cost_in_usd_ticks` | usage object | xAI |
  | `cost` | root of the response or event, beside `usage` | Eden AI |
  | `cost`, `cost_details`, `cost_in_usd_ticks` | `usage.raw_usage`, where an embeddings response and some streams carry provider usage fields | the providers above |

  These are exactly the fields GoModel reads a provider's cost from for the usage log: both use one list, so a field GoModel learns to read is removed as well. With markups off they pass through exactly as received. GoModel never adds any of them.
* **A cost field GoModel does not know cannot be removed.** A provider that reports what a request cost under another name or in another place is relayed with it, with or without markups. Before reselling a provider that is not in the table, check what its responses contain.
* Every `X-GoModel-Cost` and `X-GoModel-Cost-Currency` header from an upstream, in any letter case, is dropped; a buffered response then carries this gateway's.
* A relayed JSON or SSE answer with a `Content-Encoding` other than `identity` is refused with `502`. GoModel asks upstreams for no encoding and decodes gzip itself, so such an answer was sent unasked and cannot be read or cleaned. Other relayed content, such as audio, is not read, so it is relayed whatever its encoding.

With the setting off, responses are exactly what they were without it, an upstream's `usage.cost`, `cost_details` and cost headers included.

## Coverage and limits

| Endpoint | Buffered | Streamed |
| - | - | - |
| `/v1/chat/completions`, `/v1/responses`, `/v1/messages` | `usage.cost` and headers | `usage.cost` |
| `/v1/embeddings` | `usage.cost` and headers | — |
| `/v1/images/generations`, `/v1/images/edits`, `/v1/decisions`, `/v1/systemone` | headers, and `usage.cost` when the answer has a usage object | — |
| `/v1/audio/*` | headers | none: a relayed audio stream or streamed transcript is priced after it was sent |
| `/p/{provider}/...` | headers and `usage.cost`, for JSON answers GoModel can price | `usage.cost`, for events with OpenAI or Anthropic usage |

**What is buffered.** To price it before it answers, GoModel reads a relayed JSON answer that is a single JSON object (`/p/{provider}/...`, `/v1/decisions`, a natively forwarded `/v1/messages`) in full before it sends the first byte, instead of relaying it as it arrives. Nothing else is buffered:

* A relayed JSON answer that is not a single object, such as the JSON array Gemini's `streamGenerateContent` sends piece by piece without `alt=sse`, is relayed as it arrives and as it is. It is sent before it could be priced, so it carries no header and no `usage.cost`, and nothing inside it is edited: a cost member in such an array is not removed, with or without markups. No provider GoModel reads costs from answers this way.
* In a JSON body only a usage object is held back while it is rewritten, from its opening to its closing brace, up to 64 MiB. (With markups, so are the bytes between two top-level members, up to 4 KiB, until the name of the second is read. A longer run of whitespace there is shortened to 4 KiB, which leaves the JSON unchanged; no cost member passes through because of it.) A larger usage object is relayed as it is: it gets no cost and keeps every member the upstream put in it.
* A stream is relayed event by event. An event ends at its blank line whether its lines end in LF, CRLF or a bare CR. Only streams framed with LF or CRLF, which is what providers send, are priced: from its first event that ends another way, a stream loses upstream costs but carries none. An event carries the cost of its own usage or none: when GoModel reads no usage from it, or its provider reported its own cost (OpenRouter, xAI, Eden AI) only for the token counts of an earlier event, it has no `usage.cost`.

The remaining limits:

* A relayed JSON object over 8 MiB is unpriced: it carries no header and no `usage.cost`, and an upstream's costs are still removed. GoModel learns that it is too large only after reading the first 8 MiB, so those are sent together; the rest is relayed as it arrives.
* A relayed answer is read only when its content type is JSON (`application/json` or a `+json` type) or `text/event-stream`. One sent as anything else, such as NDJSON or plain text, and a JSON body that starts with a byte order mark, is relayed as received: it carries no cost and keeps every cost field, with or without markups.
* A stream event over 64 MiB is relayed unread, so it gets no cost and keeps whatever cost fields the upstream put in it. No provider sends usage in events that large.
* A stream event is cleaned only when its payload is well-formed JSON; one that does not parse is relayed as received.
* A JSON body is edited as it passes through and is not validated first, because that would mean holding all of it. A complete usage object is rewritten even when the body turns out to be malformed or cut off after it. A body cut off inside its usage object keeps that partial object as received; with markups, a root-level provider cost in front of it is already removed by then.

Not covered: realtime sessions (many responses per connection), batches, files and conversations (a batch's `usage` is its own), and MCP tool calls. Model calls the gateway makes itself while serving a request, such as an LLM guardrail, are recorded as their own usage rows and are not part of the returned cost. A workflow with usage tracking turned off returns none.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.