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

# Usage and cost for a single request

> Looks up one request's usage row for exact per-request cost
attribution — the per-turn complement to the whole-key aggregate
`/v1/flex/usage` returns.

`request_id` is the value of the `x-litellm-call-id` response header
on the original inference call (**not** the `id` field in the
response body). Capture that header per call, then join it here.

The lookup is scoped to the calling key: a request made with a
different key returns the same `404` as an id that does not exist.
Rows are written when a request completes, so a lookup immediately
after the call may briefly `404` — retry after a few seconds.

`total_spend` is computed through the same pricing path as the
`/v1/flex/usage` aggregates, so per-request values sum to the totals
you reconcile against. Only LiteLLM-routed (text) requests carry a
call id, so rows always have `modality=text`.




## OpenAPI

````yaml /inference-api/openapi.yaml get /v1/flex/usage/request/{request_id}
openapi: 3.1.0
info:
  title: FlexAI Inference API
  version: 1.0.0
  description: |
    OpenAI-compatible inference API for text, code, reasoning, vision, and
    embedding models hosted by FlexAI. Use any OpenAI SDK by pointing the
    `base_url` at this service.
  contact:
    name: FlexAI Support
    email: support@flex.ai
    url: https://docs.flex.ai/inference-api/overview
servers:
  - url: https://api.flex.ai
    description: Production
  - url: https://tokens.flex.ai
    description: Production (legacy host, still supported)
security:
  - bearerAuth: []
tags:
  - name: Chat
    description: Chat completions (streaming, tool use, vision).
  - name: Completions
    description: Legacy text completions.
  - name: Embeddings
    description: Generate vector embeddings for text.
  - name: Models
    description: Discover available models.
  - name: Usage
    description: Programmatic spend and quota for the calling key.
paths:
  /v1/flex/usage/request/{request_id}:
    get:
      tags:
        - Usage
      summary: Usage and cost for a single request
      description: |
        Looks up one request's usage row for exact per-request cost
        attribution — the per-turn complement to the whole-key aggregate
        `/v1/flex/usage` returns.

        `request_id` is the value of the `x-litellm-call-id` response header
        on the original inference call (**not** the `id` field in the
        response body). Capture that header per call, then join it here.

        The lookup is scoped to the calling key: a request made with a
        different key returns the same `404` as an id that does not exist.
        Rows are written when a request completes, so a lookup immediately
        after the call may briefly `404` — retry after a few seconds.

        `total_spend` is computed through the same pricing path as the
        `/v1/flex/usage` aggregates, so per-request values sum to the totals
        you reconcile against. Only LiteLLM-routed (text) requests carry a
        call id, so rows always have `modality=text`.
      operationId: getUsageRequest
      parameters:
        - name: request_id
          in: path
          required: true
          schema:
            type: string
          description: The `x-litellm-call-id` header value from the original response.
      responses:
        '200':
          description: Successful response
          content:
            application/json:
              schema:
                type: object
                required:
                  - object
                  - currency
                  - request_id
                  - model
                  - canonical_model
                  - status
                  - prompt_tokens
                  - completion_tokens
                  - total_tokens
                  - total_spend
                  - modality
                  - billing_unit
                  - started_at
                  - ended_at
                  - generated_at
                properties:
                  object:
                    type: string
                    enum:
                      - usage.request
                  currency:
                    type: string
                    enum:
                      - USD
                  request_id:
                    type: string
                  model:
                    type: string
                    description: >-
                      Raw ledger id — the served routing target; not stable
                      across packaging changes.
                  canonical_model:
                    type: string
                    description: >-
                      Stable catalog id; use this to group and reconcile
                      per-model.
                  status:
                    type: string
                    description: |
                      `success`, `failure`, or `unknown`. Failed requests
                      typically report zero tokens and $0 spend.
                  prompt_tokens:
                    type: integer
                  completion_tokens:
                    type: integer
                  total_tokens:
                    type: integer
                  total_spend:
                    type: number
                  modality:
                    type: string
                    enum:
                      - text
                  units_consumed:
                    type: number
                    nullable: true
                  billing_unit:
                    type: string
                  started_at:
                    type: string
                    format: date-time
                  ended_at:
                    type: string
                    format: date-time
                  generated_at:
                    type: string
                    format: date-time
        '401':
          $ref: '#/components/responses/Unauthorized'
        '404':
          description: |
            No request with this id is visible to this key — unknown id, a
            request made with a different key, or a row not yet written.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '503':
          description: Authentication service temporarily unavailable.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '504':
          description: Usage query timed out. Retry shortly.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
components:
  responses:
    Unauthorized:
      description: Missing or invalid API key.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
  schemas:
    Error:
      type: object
      required:
        - error
      properties:
        error:
          type: object
          required:
            - message
            - type
          properties:
            message:
              type: string
            type:
              type: string
              example: invalid_request_error
            code:
              type: string
              description: |
                Stable machine-readable code for the failure. Present on
                most error paths; OMITTED on FastAPI default-validation
                bodies. Do not assume always present.
            doc_url:
              type: string
              format: uri
              description: |
                FlexAI extension. Link to the relevant dashboard / docs
                page for the failure mode (e.g. invalid key → /dashboard/keys).
                Present on auth and quota errors.
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: sk-xxxx
      description: |
        Virtual API key. Create one from the
        [FlexAI dashboard](https://tokens.flex.ai/dashboard/keys). Pass as
        `Authorization: Bearer sk-xxxx`.

````