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.
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
| File | Contains | Use For |
|---|---|---|
SKILL.md | Entry point: scope, routing table, and workflow. | Start here. |
docs/counting-tokens-openai-api-workflow-guide.md | A 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.text | A 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.text | A 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.text | A 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.text | A 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.text | A 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.text | A 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.text | A 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.text | A 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.text | A 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.text | A 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.text | A 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.text | A 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.text | A 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.text | A 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.text | A 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.text | A 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.text | A 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.text | A 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.text | A 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.text | A 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.text | A 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
- Open the most relevant file under
docs/for the exact documented workflow and wording. - Open
schemas/files for exact structured contracts. - Open
examples/files for concrete requests, commands, snippets, and manifests. - 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
