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

# Count Tokens

> Google-compatible countTokens endpoint for pre-flight context-window sizing. The request body matches `:generateContent`. NOTE: Opper has no in-process Gemini tokenizer and returns a chars/4 heuristic (±~20% on English prose) — sufficient for "does my prompt fit?" checks but not for exact cost projection.



## OpenAPI

````yaml post /v3/compat/v1beta/models/{model}:countTokens
openapi: 3.1.0
info:
  description: Schema-driven generative API that orchestrates LLM-powered workflows.
  title: Task API
  version: 3.0.0
servers:
  - description: Production
    url: https://api.opper.ai
  - description: Local development
    url: http://localhost:8080
security:
  - BearerAuth: []
tags:
  - description: Schema-driven function management and execution
    name: Functions
  - description: OpenAI-compatible chat completions
    name: Chat
  - description: OpenAI Responses API compatible endpoint
    name: Responses
  - description: Google-compatible interactions endpoint
    name: Interactions
  - description: Model registry and capabilities
    name: Models
  - description: Synchronous image generation
    name: Images
  - description: Text-to-speech and speech-to-text
    name: Audio
  - description: Asynchronous video generation
    name: Videos
  - description: Reusable file storage for media inputs and generated outputs
    name: Files
  - description: Async generation status and downloads
    name: Artifacts
  - description: OpenAI-compatible embeddings
    name: Embeddings
  - description: Recorded HTTP request/response generations
    name: Generations
  - description: System health and status
    name: System
  - description: Roundtable endpoint — fan out a query to multiple LLMs and combine results
    name: Roundtable
  - description: Web search, fetch, and other utility tools
    name: Tools
paths:
  /v3/compat/v1beta/models/{model}:countTokens:
    post:
      tags:
        - Compatibility
      summary: Count Tokens API
      description: >-
        Google-compatible countTokens endpoint for pre-flight context-window
        sizing. The request body matches `:generateContent`. NOTE: Opper has no
        in-process Gemini tokenizer and returns a chars/4 heuristic (±~20% on
        English prose) — sufficient for "does my prompt fit?" checks but not for
        exact cost projection.
      operationId: countTokens
      parameters:
        - description: >-
            Model id — a bare Gemini name (e.g. `gemini-2.5-flash`, resolved
            against the Gemini-direct provider) or an Opper-canonical id (e.g.
            `vertexai/gemini-2.5-flash`).
          in: path
          name: model
          required: true
          schema:
            type: string
        - description: >-
            Function name for tracing and project-level guardrail function-scope
            filtering.
          in: header
          name: X-Opper-Name
          schema:
            type: string
        - description: Parent span ID for distributed tracing context.
          in: header
          name: X-Opper-Parent-Span-Id
          schema:
            format: uuid
            type: string
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/InteractionsRequest'
        required: true
      responses:
        '200':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/CountTokensResponse'
          description: Successful response
        '400':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
          description: Bad request
        '401':
          description: Unauthorized - missing or invalid API key
        '500':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
          description: Internal server error
components:
  schemas:
    InteractionsRequest:
      properties:
        agent:
          type: string
        background:
          type: boolean
        contents:
          description: Any value
        custom_model_credentials:
          properties:
            api_key:
              type: string
            base_url:
              type: string
            extra:
              type: object
            identifier:
              type: string
            url:
              type: string
          required:
            - identifier
            - api_key
          type: object
        generation_config:
          properties:
            max_output_tokens:
              type: integer
            response_mime_type:
              type: string
            response_schema:
              type: object
            seed:
              type: integer
            stop_sequences:
              items:
                type: string
              type: array
            temperature:
              type: number
            thinking_config:
              properties:
                include_thoughts:
                  type: boolean
                thinking_budget:
                  type: integer
                thinking_level:
                  type: string
              type: object
            thinking_level:
              type: string
            thinking_summaries:
              type: string
            tool_choice:
              description: Any value
            top_k:
              type: integer
            top_p:
              type: number
          type: object
        input:
          description: Any value
        model:
          type: string
        previous_interaction_id:
          type: string
        response_format:
          type: object
        response_mime_type:
          type: string
        safety_settings:
          items:
            properties:
              category:
                type: string
              threshold:
                type: string
            type: object
          type: array
        store:
          type: boolean
        stream:
          type: boolean
        system_instruction:
          properties:
            Text:
              type: string
          required:
            - Text
          type: object
        tool_config:
          properties:
            function_calling_config:
              properties:
                allowed_function_names:
                  items:
                    type: string
                  type: array
                mode:
                  type: string
              type: object
          type: object
        tools:
          items:
            properties:
              codeExecution:
                description: Any value
              code_execution:
                description: Any value
              description:
                type: string
              function_declarations:
                items:
                  properties:
                    description:
                      type: string
                    name:
                      type: string
                    parameters:
                      type: object
                  required:
                    - name
                  type: object
                type: array
              googleSearch:
                description: Any value
              googleSearchRetrieval:
                description: Any value
              google_search:
                description: Any value
              google_search_retrieval:
                description: Any value
              name:
                type: string
              parameters:
                type: object
              type:
                type: string
              urlContext:
                description: Any value
              url_context:
                description: Any value
            type: object
          type: array
      required:
        - input
        - contents
      type: object
    CountTokensResponse:
      properties:
        cachedContentTokenCount:
          type: integer
        totalTokens:
          type: integer
      required:
        - totalTokens
      type: object
    ErrorResponse:
      properties:
        error:
          properties:
            code:
              type: string
            details:
              description: Any value
            message:
              type: string
          required:
            - code
            - message
          type: object
        meta:
          type: object
      required:
        - error
      type: object
  securitySchemes:
    BearerAuth:
      bearerFormat: API Key
      description: API key authentication. Pass your API key as a Bearer token.
      scheme: bearer
      type: http

````