Anthropic CountTokens

Anthropic CountTokens

Certified

Count input tokens for a Claude request

Calls the Anthropic token-count endpoint (POST /v1/messages/count_tokens) with the same rendered messages, system prompt, tools, and model as ChatCompletion. Returns only inputTokens. Does not create a message or call the model. A later task can read {{ outputs.<task-id>.inputTokens }} to gate on cost or context size before ChatCompletion. Refer to the Anthropic Console Settings to create an API key and the Anthropic API documentation for more information.

yaml
type: io.kestra.plugin.anthropic.CountTokens

Count tokens using Claude.

yaml
id: anthropic_count_tokens
namespace: company.team

tasks:
  - id: count_tokens
    type: io.kestra.plugin.anthropic.CountTokens
    apiKey: "{{ secret('ANTHROPIC_API_KEY') }}"
    model: "claude-sonnet-4-6"
    messages:
      - type: USER
        content: "What is the capital of Japan? Answer with a unique word and without any punctuation."

Count tokens with tools

yaml
id: anthropic_count_tokens_with_tools
namespace: company.team

tasks:
  - id: count_tokens
    type: io.kestra.plugin.anthropic.CountTokens
    apiKey: "{{ secret('ANTHROPIC_API_KEY') }}"
    model: "claude-sonnet-4-6"
    messages:
      - type: USER
        content: |
          Extract the following information from this text:
          "John Doe is 30 years old and works as a Software Engineer in San Francisco."
    tools:
      - name: extract_person_info
        description: "Extract structured information about a person"
        input_schema:
          type: object
          properties:
            name:
              type: string
              description: "The person's full name"
            age:
              type: integer
              description: "The person's age"
            occupation:
              type: string
              description: "The person's job title"
            location:
              type: string
              description: "The person's location"
          required:
            - name
            - age

Count tokens, then complete only when the prompt fits.

yaml
id: anthropic_count_tokens_before_completion
namespace: company.team

tasks:
  - id: count_tokens
    type: io.kestra.plugin.anthropic.CountTokens
    apiKey: "{{ secret('ANTHROPIC_API_KEY') }}"
    model: "claude-sonnet-4-6"
    system: "Answer in one word."
    messages:
      - type: USER
        content: "What is the capital of Japan? Answer with a unique word and without any punctuation."

  - id: chat_completion
    type: io.kestra.plugin.anthropic.ChatCompletion
    runIf: "{{ outputs.count_tokens.inputTokens < 8000 }}"
    apiKey: "{{ secret('ANTHROPIC_API_KEY') }}"
    model: "claude-sonnet-4-6"
    maxTokens: 1024
    system: "Answer in one word."
    messages:
      - type: USER
        content: "What is the capital of Japan? Answer with a unique word and without any punctuation."
Properties

Anthropic API Key

Messages

Ordered chat turns rendered from properties; include at least one USER message.

Definitions
contentstring
typestring
Possible Values
ASSISTANTUSER

Model

Claude model name used to estimate tokens (e.g., claude-sonnet-4-6); must match an Anthropic model available to your API key.

Assets this task consumes as inputs or produces as outputs, for lineage tracking and the asset graph (Enterprise Edition). A flow declaring this property on a task is rejected in the open-source edition.

Definitions
assetFailureBehaviorstring
Possible Values
IGNOREFAILWARN

Asset failure behavior

Behavior applied to the task state when a declared asset fails to render, emit, or be persisted (e.g. a lock conflict): FAIL escalates it to FAILED, WARN (default) warns it if it would otherwise succeed, IGNORE leaves the state untouched.

enableAutobooleanstring

Whether to auto-register assets referenced dynamically at runtime that are not statically declared in inputs or outputs.

inputsarray

The assets consumed as inputs.

id*string
Min length1
typestring
outputs

The assets produced as outputs.

id*string
Min length1
Max length150
type*object
descriptionstring
displayNamestring
metadataobject
Default{}
namespacestring
Min length1
Max length150
id*string
Min length1
Max length150
type*object
descriptionstring
displayNamestring
metadataobject
Default{}
namespacestring
Min length1
Max length150
id*string
Min length1
Max length150
type*object
descriptionstring
displayNamestring
metadataobject
Default{}
namespacestring
Min length1
Max length150
id*string
Min length1
Max length150
type*object
descriptionstring
displayNamestring
metadataobject
Default{}
namespacestring
Min length1
Max length150
id*string
Min length1
Max length150
type*object
descriptionstring
displayNamestring
metadataobject
Default{}
namespacestring
Min length1
Max length150
id*string
Min length1
Max length150
type*string
Min length1

Custom asset type

descriptionstring
displayNamestring
metadataobject
Default{}
namespacestring
Min length1
Max length150

System prompt

Optional system instructions applied to the whole conversation; rendered before the token count.

Tools

Optional tools included in the estimate; each entry needs a unique name, an optional description, and an input_schema JSON Schema that defines the parameters the tool accepts. Tools are counted, not invoked.

Definitions
descriptionstring

Tool description

Optional description of what the tool does.

inputSchemaobject

Input schema

JSON Schema object defining the expected parameters for the tool.

namestring

Tool name

Unique identifier for the tool (1-128 characters).

Input tokens

Estimated number of input tokens for the given messages, system prompt, and tools.

Unittoken

Number of input tokens estimated for the request.