---
title: "Count tokens in a Message"
method: POST
path: "/v1/messages/count_tokens"
tags: ["messages"]
---

# Count tokens in a Message

`POST /v1/messages/count_tokens`

Count tokens in a Message

## Request body

- CountTokensInput — Anthropic /messages/count_tokens payload.
  - `model` string, required — Model identifier or alias.
  - `messages` MessageInput[], required — Conversation messages for the request.
    - `role` 'system' | 'user' | 'assistant', required — The role of the message sender
    - `content` union, required — The message content
      - string
      - MessageInputContentOneOf1Items[]
        - union
          - object — audio variant
            - `type` 'audio', required — Discriminator value: audio
          - object — binary variant
            - `type` 'binary', required — Discriminator value: binary
          - object — container_upload variant
            - `type` 'container_upload', required — Discriminator value: container_upload
          - object — document variant
            - `type` 'document', required — Discriminator value: document
          - object — image variant
            - `type` 'image', required — Discriminator value: image
          - object — local_resource variant
            - `type` 'local_resource', required — Discriminator value: local_resource
          - object — mid_conv_system variant
            - `type` 'mid_conv_system', required — Discriminator value: mid_conv_system
          - object — redacted_thinking variant
            - `type` 'redacted_thinking', required — Discriminator value: redacted_thinking
          - object — resource variant
            - `type` 'resource', required — Discriminator value: resource
          - object — resource_link variant
            - `type` 'resource_link', required — Discriminator value: resource_link
          - object — search_result variant
            - `type` 'search_result', required — Discriminator value: search_result
          - object — server_tool_use variant
            - `type` 'server_tool_use', required — Discriminator value: server_tool_use
          - object — source variant
            - `type` 'source', required — Discriminator value: source
          - object — text variant
            - `type` 'text', required — Discriminator value: text
          - object — thinking variant
            - `type` 'thinking', required — Discriminator value: thinking
          - object — tldr variant
            - `type` 'tldr', required — Discriminator value: tldr
          - object — tool_result variant
            - `type` 'tool_result', required — Discriminator value: tool_result
          - object — tool_use variant
            - `type` 'tool_use', required — Discriminator value: tool_use
  - `system` System[] — System prompt input. Accepts str, list[str], System, list[System], or null. It is normalized internally to list[System].
    - `use_default_prompt` boolean — Deprecated: legacy toggle for built-in default prompt injection. Use system.prompt to control per-category prompt injection.
    - `text` string, nullable — System prompt to use for the chat completion
    - `citations` Citations — Configuration for citation generation in AI responses.
      - `enabled` boolean — Enable citations in responses
      - `known_citations` ZylonCitation[], nullable — List of known citations to use in the response
        - `id` string, nullable — Unique identifier for the citation
        - `index` integer, nullable — Index of the citation in the document
        - `artifact_id` string, nullable — Identifier of the artifact associated with the citation
        - `source_id` string, nullable — Identifier of the source document from which the citation is derived
    - `extensions` SystemExtensions[] — Set of enabled extensions
    - `blob_visibility` 'binary' | 'internal' | 'public' — Controls visibility and storage mode for binary large objects (blobs).
    - `prompt` PromptConfig — Controls which platform-level prompt features are injected. These flags represent optional AI features adding internal instructions to the system prompt. All flags default to ``False`` — opt-in explicitly.
      - `tools` boolean — Enable per-tool instruction injection for all available tools.
      - `citations` boolean — Enable citation formatting guidelines injection.
      - `thinking` boolean — Enable thinking/reasoning guidelines when thinking is enabled.
      - `code_execution` boolean — Enable code execution environment instructions (filesystem layout, available paths) when any code execution tool is present.
      - `skills` boolean — Enable skill management instructions (when to load/unload skills, workflow guidance) when any skill management tool is present.
  - `tools` ToolSpecBody[], nullable — Optional tool definitions.
    - `name` string, required — Unique name identifier for the tool
    - `type` string, nullable — Type of the tool, use to identify internal tools database_query_v1 or semantic_search_v1
    - `description` string, nullable — Human-readable description of what the tool does
    - `inputSchema` object, nullable — JSON schema defining the input parameters the tool accepts
    - `context` ToolSpecBodyContextItems[], nullable — Additional context or metadata for the tool
      - union
        - object — Input for base64 encoded files.
          - `type` 'file', required — Discriminator value: file
          - `value` string, required — Base64 encoded file content
        - object — Input for already ingested content.
          - `type` 'ingested_artifact', required — Discriminator value: ingested_artifact
          - `context_filter` ContextFilter, required — Filter by collection, artifacts and metadata in the ingested context. The main filter is the collection the context is part of. All artifacts ids are ensured to be part of the context. If any of the ids is not usable in the context (i.e. doesn't exist), the related task will fail. The metadata filter will look for all context matching the metadata, if any, and add it to the context. The filter is a dictionary of key-value pairs. The key is the metadata key, and the value is the metadata value. If both artifacts and metadata filters are provided, the result will be the intersection of the two filters.
            - `collection` string — The name of the collection to filter on.
            - `artifacts` string[], nullable — Artifacts ids to filter on.
            - `metadata_filter` object[], nullable — Metadata filter to apply on the context.
              - …
        - object — Input for skill activation using SkillFilter.
          - `type` 'skill', required — Discriminator value: skill
          - `skill_filter` SkillFilter, required — Collection-scoped filter used to resolve active skills and versions.
            - `collection` string, required — Tenant collection boundary used to recover skills.
            - `skill_or_version_ids` string[], nullable — Optional whitelist of identifiers inside collection. Each item may be a skill id (resolved to latest version) or a skill version id.
        - object — Input for SQL database content.
          - `type` 'sql_database', required — Discriminator value: sql_database
          - `connection_string` string, required — SQL database connection string
          - `schemas` string[], nullable — List of specific schemas to include, if None includes all non-system schemas
          - `ssl` boolean — Whether to use SSL for the database connection
          - `enable_tables` boolean — Whether to include tables in the inspection
          - `enable_views` boolean — Whether to include views in the inspection
          - `enable_functions` boolean — Whether to include functions in the inspection
          - `enable_procedures` boolean — Whether to include stored procedures in the inspection
          - `description` string — Optional description of the database
        - object — Input for plain text content.
          - `type` 'text', required — Discriminator value: text
          - `value` string, required — Plain text content
        - object — Input for remote URIs.
          - `type` 'uri', required — Input type discriminator
          - `value` string, required — URI to download from
    - `deferLoading` boolean — When true, hide this tool from the model until at least one skill is loaded in the current conversation.
    - `instructions` string, nullable — Optional instructions injected into the system prompt when this tool is available. For internal tools a default template is used; providing a value here overrides that default. Set to an empty string to disable.
  - `thinking` Thinking — Configuration for AI reasoning and step-by-step thinking capabilities.
    - `enabled` boolean — Enable reasoning capabilities for the model, allowing it to think step-by-step
    - `effort` 'low' | 'medium' | 'high' | 'max' | 'xhigh' — Deprecated. Use output_config.effort instead. Kept for backward compatibility with legacy clients.
  - `tool_choice` ToolChoice — Configuration for tool selection behavior during AI interactions.
    - `type` 'auto' | 'any' | 'tool' | 'none' — Tool selection strategy
    - `name` string, nullable — Name of the tool to use if not auto-selecting
    - `disable_parallel_tool_use` boolean — When true, prevents the AI from using multiple tools simultaneously
    - `validation_mode` 'eager' | 'lazy' — Tool validation mode. 'eager' validates tool calls before execution, 'lazy' validates if tool call is made.
  - `output_config` OutputConfigInput — Output configuration shared across Anthropic-compatible request models.
    - `effort` 'low' | 'medium' | 'high' | 'max' | 'xhigh' — Reasoning effort level for output generation.
    - `format` JsonObjectFormat — Structured JSON object format compatible with Anthropic output_config.format.
      - `type` 'json_schema', required — Output format type. Always "json_schema".
      - `schema` object, required — JSON schema used to constrain the model output.
  - `cache_control` CountTokensInputCacheControl — CacheControlEphemeral schema.
    - `type` 'ephemeral', required — Discriminator value: ephemeral
    - `ttl` '5m' | '1h' — ttl field.

## Response `200`

Successful Response

- CountTokensOutput — Token count payload.
  - `input_tokens` integer, required — Estimated number of input tokens for the provided payload.

## Other responses

- `401` — Unauthorized
- `422` — Validation Error

---

[API](https://skmtc.dev/privategpt/apis/api-reference.md) · [All operations](https://skmtc.dev/privategpt/apis/api-reference/llms.txt) · [OpenAPI document](https://skmtc-service-production.skmtc.workers.dev/v1/apis/privategpt/api-reference/revisions/15d9ccfd68da/schema)
