> ## 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.

# Programmatic spend for the calling key

> FlexAI-specific shape (intentionally not OpenAI's `/v1/organization/usage/*`
— that surface requires org/team scoping which FlexAI does not yet model).
Returns total spend, total tokens, per-model breakdown, and a daily
history for the API key in the `Authorization` header. Cached per-key
for 60 seconds. The `total_spend` value reconciles against your
invoices within 1% tolerance.




## OpenAPI

````yaml /inference-api/openapi.yaml get /v1/flex/usage
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://tokens.flex.ai
    description: Production
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:
    get:
      tags:
        - Usage
      summary: Programmatic spend for the calling key
      description: >
        FlexAI-specific shape (intentionally not OpenAI's
        `/v1/organization/usage/*`

        — that surface requires org/team scoping which FlexAI does not yet
        model).

        Returns total spend, total tokens, per-model breakdown, and a daily

        history for the API key in the `Authorization` header. Cached per-key

        for 60 seconds. The `total_spend` value reconciles against your

        invoices within 1% tolerance.
      operationId: getUsage
      parameters:
        - name: days
          in: query
          required: false
          schema:
            type: integer
            minimum: 1
            maximum: 365
            default: 30
          description: Number of trailing days to include in `daily`.
      responses:
        '200':
          description: Successful response
          content:
            application/json:
              schema:
                type: object
                required:
                  - object
                  - currency
                  - total_spend
                  - total_tokens
                  - total_requests
                  - by_model
                  - daily
                  - generated_at
                properties:
                  object:
                    type: string
                    enum:
                      - usage
                  currency:
                    type: string
                    enum:
                      - USD
                  total_spend:
                    type: number
                  total_tokens:
                    type: integer
                  total_requests:
                    type: integer
                  by_model:
                    type: array
                    description: >
                      Per-model spend rows. Multi-modality models appear once
                      per

                      modality (image+image_edit, etc.) — use `model`+`modality`
                      as

                      the composite key. Text rows have `modality=text`,

                      `units_consumed=null`, `billing_unit=tokens`.
                    items:
                      type: object
                      required:
                        - model
                        - modality
                        - requests
                        - total_tokens
                        - billing_unit
                        - total_spend
                      properties:
                        model:
                          type: string
                        modality:
                          type: string
                          enum:
                            - text
                            - image
                            - image_edit
                            - video
                            - audio
                            - tts
                            - stt
                        requests:
                          type: integer
                        total_tokens:
                          type: integer
                        units_consumed:
                          type: number
                          nullable: true
                        billing_unit:
                          type: string
                        total_spend:
                          type: number
                  daily:
                    type: array
                    items:
                      type: object
                      required:
                        - date
                        - requests
                        - total_tokens
                        - total_spend
                      properties:
                        date:
                          type: string
                          format: date
                        requests:
                          type: integer
                        total_tokens:
                          type: integer
                        total_spend:
                          type: number
                  generated_at:
                    type: string
                    format: date-time
        '401':
          $ref: '#/components/responses/Unauthorized'
        '422':
          description: |
            `?days` outside the [1, 365] range. Returns FastAPI's default
            validation envelope (not the OpenAI shape used elsewhere on
            this route). SDK clients should clamp client-side.
          content:
            application/json:
              schema:
                type: object
                properties:
                  detail:
                    type: array
                    items:
                      type: object
        '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`.

````