Prompt Buddy logoPrompt Buddy

openai · OpenAI Platform Docs

Counting tokens | OpenAI API

Explains how to calculate token counts for text inputs to manage model context limits and estimate costs.

Import to Prompt Buddy

Derived skill

Files assembled from official documentation

Viewing SKILL.md

Counting tokens | OpenAI API

Explains how to calculate token counts for text inputs to manage model context limits and estimate costs.

When To Use

Use when you need to estimate API costs, prevent context window overflows, or truncate text inputs before sending them to a model.

Reference Files

FileContainsUse For
SKILL.mdEntry point: scope, routing table, and workflow.Start here.
docs/counting-tokens-openai-api-workflow-guide.mdA guide explaining how to use the OpenAI API to estimate input token usage for messages, conversations, and instructions to optimize costs and context limits.Questions about a guide explaining how to use the OpenAI API to estimate input token usage for messages, conversations, and instructi...
examples/counting-tokens-openai-api-openai-api-python-count-tokens.textA Python script demonstrating how to use the OpenAI client to count input tokens for a given model and text string.Exact payloads, commands, or snippets shown in A Python script demonstrating how to use the OpenAI client to count input tokens for a given model and text string.
examples/counting-tokens-openai-api-openai-api-count-tokens-javascript.textA JavaScript code snippet demonstrating how to use the OpenAI client to count input tokens for a specific model.Exact payloads, commands, or snippets shown in A JavaScript code snippet demonstrating how to use the OpenAI client to count input tokens for a specific model.
examples/counting-tokens-openai-api-openai-api-input-token-count-curl-request.textA curl command demonstrating how to send a request to the OpenAI API to count input tokens for a specific model and text string.Exact payloads, commands, or snippets shown in A curl command demonstrating how to send a request to the OpenAI API to count input tokens for a specific model and t...
examples/counting-tokens-openai-api-openai-api-token-counting-cli.textA command-line interface example demonstrating how to count input tokens for a specific model using the OpenAI API.Exact payloads, commands, or snippets shown in A command-line interface example demonstrating how to count input tokens for a specific model using the OpenAI API.
examples/counting-tokens-openai-api-openai-api-python-input-token-count.textA Python script using the OpenAI client to count input tokens for a multi-turn conversation using the inputtokens.count method.Exact payloads, commands, or snippets shown in A Python script using the OpenAI client to count input tokens for a multi-turn conversation using the inputtokens.cou...
examples/counting-tokens-openai-api-openai-api-inputtokens-count-nodejs.textA Node.js code example demonstrating how to use the OpenAI client to count input tokens for a conversation history.Exact payloads, commands, or snippets shown in A Node.js code example demonstrating how to use the OpenAI client to count input tokens for a conversation history.
examples/counting-tokens-openai-api-openai-api-inputtokens-curl-request.textA curl command demonstrating how to send a request to the OpenAI input tokens endpoint to count tokens in a conversation history.Exact payloads, commands, or snippets shown in A curl command demonstrating how to send a request to the OpenAI input tokens endpoint to count tokens in a conversat...
examples/counting-tokens-openai-api-openai-api-token-counting-cli-2.textA CLI command demonstration using the raw output flag and YAML input to count tokens for a multi-turn conversation.Exact payloads, commands, or snippets shown in A CLI command demonstration using the raw output flag and YAML input to count tokens for a multi-turn conversation.
examples/counting-tokens-openai-api-openai-api-python-input-token-count-2.textA Python script demonstrating how to use the OpenAI client to count input tokens for a specific model and prompt.Exact payloads, commands, or snippets shown in A Python script demonstrating how to use the OpenAI client to count input tokens for a specific model and prompt.
examples/counting-tokens-openai-api-openai-api-count-input-tokens-javascript.textA JavaScript code snippet demonstrating how to use the OpenAI client to count input tokens for a specific model and prompt.Exact payloads, commands, or snippets shown in A JavaScript code snippet demonstrating how to use the OpenAI client to count input tokens for a specific model and p...
examples/counting-tokens-openai-api-openai-api-inputtokens-curl-request-2.textA curl command demonstrating how to send a request to the OpenAI input tokens endpoint to count tokens for a specific model and input string.Exact payloads, commands, or snippets shown in A curl command demonstrating how to send a request to the OpenAI input tokens endpoint to count tokens for a specific...
examples/counting-tokens-openai-api-openai-api-token-counting-cli-3.textA text file demonstrating how to use the OpenAI CLI to count input tokens for a specific model and prompt using a YAML configuration.Exact payloads, commands, or snippets shown in A text file demonstrating how to use the OpenAI CLI to count input tokens for a specific model and prompt using a YAM...
examples/counting-tokens-openai-api-openai-api-python-input-token-count-3.textA Python code example demonstrating how to use the OpenAI client to count input tokens for a request containing both text and an image URL.Exact payloads, commands, or snippets shown in A Python code example demonstrating how to use the OpenAI client to count input tokens for a request containing both...
examples/counting-tokens-openai-api-openai-api-nodejs-input-token-count.textA Node.js code snippet demonstrating how to use the OpenAI client to count input tokens for a multimodal request containing an image and text.Exact payloads, commands, or snippets shown in A Node.js code snippet demonstrating how to use the OpenAI client to count input tokens for a multimodal request cont...
examples/counting-tokens-openai-api-openai-api-inputtokens-curl-request-3.textA curl command demonstrating how to send a request to the OpenAI input tokens endpoint using a model and multimodal input.Exact payloads, commands, or snippets shown in A curl command demonstrating how to send a request to the OpenAI input tokens endpoint using a model and multimodal i...
examples/counting-tokens-openai-api-openai-api-token-counting-cli-raw.textA text example demonstrating how to use the OpenAI CLI with the --transform flag to count input tokens for a multimodal request.Exact payloads, commands, or snippets shown in A text example demonstrating how to use the OpenAI CLI with the --transform flag to count input tokens for a multimod...
examples/counting-tokens-openai-api-openai-api-python-count-input-tokens.textA Python code example using the OpenAI client to count input tokens for a model including tool definitions.Exact payloads, commands, or snippets shown in A Python code example using the OpenAI client to count input tokens for a model including tool definitions.
examples/counting-tokens-openai-api-openai-api-nodejs-input-token-count-2.textA Node.js code snippet demonstrating how to use the OpenAI client to count input tokens for a model including tool definitions.Exact payloads, commands, or snippets shown in A Node.js code snippet demonstrating how to use the OpenAI client to count input tokens for a model including tool de...
examples/counting-tokens-openai-api-openai-api-inputtokens-curl-request-4.textA curl command demonstrating how to request input token counts from the OpenAI API using a JSON payload.Exact payloads, commands, or snippets shown in A curl command demonstrating how to request input token counts from the OpenAI API using a JSON payload.
examples/counting-tokens-openai-api-openai-api-token-counting-cli-raw-2.textA text representation of raw CLI output showing input token counts for a model request containing tool definitions.Exact payloads, commands, or snippets shown in A text representation of raw CLI output showing input token counts for a model request containing tool definitions.
examples/counting-tokens-openai-api-openai-api-token-counting-request.textA text representation of a POST request to the OpenAI API for counting input tokens.Exact payloads, commands, or snippets shown in A text representation of a POST request to the OpenAI API for counting input tokens.

What This Skill Covers

  • Token counting lets you determine how many input tokens a request will use before you send it to the model. Use it to:
  • Main sections: Why use the token counting API?, Count tokens in basic messages, Count tokens in conversations, Count tokens with instructions, Count tokens with images.

Workflow

  1. Open the most relevant file under docs/ for the exact documented workflow and wording.
  2. Open schemas/ files for exact structured contracts.
  3. Open examples/ files for concrete requests, commands, snippets, and manifests.
  4. Do not add behavior or configuration that is not present in the attached source files.

Canonical source: https://developers.openai.com/api/docs/guides/token-counting