59API

← Retour aux guides

Tokens Explained: Count AI API Costs Precisely

Tarifs · EN · 2026-09-19

What Is an AI Token?

A token is a piece of text that an AI model processes. It may be a whole word, part of a word, punctuation, or whitespace. For example, the sentence Tokens make billing measurable. might be split into several tokens rather than three complete words. The exact split depends on the model and its tokenizer.

As a practical English estimate, one token is often about four characters or roughly three-quarters of a word. This is only a planning shortcut. Code, JSON, URLs, non-English languages, long numbers, and unusual formatting can produce many more tokens than the estimate suggests.

What Gets Counted?

For a chat request, token usage usually includes both the input and the output. Input tokens include system instructions, conversation history, tool definitions, retrieved documents, and your latest message. Output tokens include the model's generated answer. Some providers price input and output at different rates, so tracking them separately matters.

Consider a support bot that receives a 1,000-token system prompt, a 2,000-token conversation history, and a 300-token question. Its input is already about 3,300 tokens before the model generates anything. If the response is 700 tokens, the request uses approximately 4,000 tokens in total. Sending the full history on every turn can therefore become more expensive than the user's new question.

Count Tokens Before Sending a Request

For GPT models, a tokenizer such as tiktoken can provide a local estimate. Install it with pip install tiktoken, then run:

Python: import tiktoken; encoding = tiktoken.encoding_for_model('gpt-4o'); text = 'Summarize this incident report in five bullets.'; print(len(encoding.encode(text)))

This counts the text itself. A real chat request also contains message roles, system content, tool schemas, and protocol overhead, so the API's usage fields are the final authority. For a rough budget, count every piece of content you send, then reserve room for the expected response.

In JavaScript, the official OpenAI SDK returns usage information after completion. A request through 59API can look like this:

import OpenAI from 'openai'; const client = new OpenAI({ apiKey: process.env.API_KEY, baseURL: 'https://api.59api.com/v1' }); const result = await client.chat.completions.create({ model: 'gpt-4o-mini', messages: [{ role: 'user', content: 'Explain this error in plain English.' }] }); console.log(result.usage.prompt_tokens, result.usage.completion_tokens, result.usage.total_tokens);

Keep the usage object in your logs. Recording model name, input tokens, output tokens, latency, and request ID makes it possible to find unusually large prompts and calculate real costs instead of relying on averages.

Claude and Provider-Specific Counting

Claude models use their own tokenizer, so a GPT tokenizer is not guaranteed to produce an exact Claude count. Anthropic provides token-counting capabilities for supported models and request formats. When exact budgeting matters, count with the tokenizer or counting endpoint intended for the model you will actually call.

The same rule applies when using a relay. 59API provides access to native Claude Opus, Sonnet, Haiku, and Fable models as well as GPT models through an OpenAI-compatible API. Choose the target model first, then measure usage from the returned API metadata. Do not assume that a prompt's token count is identical across model families.

Three Ways to Reduce Token Costs

Also watch for accidental duplication. A retrieved document pasted into both the system prompt and the user message is billed twice. For large files, retrieve only the relevant sections instead of sending the entire file.

Why Token Counting Matters with 59API

Token counting lets you compare model quality against actual spend. 59API is a low-cost, pay-as-you-go relay with an API base URL of https://api.59api.com, native official-quality models without a downgrade, and compatibility with Claude Code, Codex, and OpenAI SDKs. That makes it useful for testing the same measured workload across Claude and GPT models while keeping billing usage-based. Developers can also use its referral rebate to reduce effective costs over time.

Start by signing up for 59API, send a small representative request, and record the returned input and output token counts. Once you know the average tokens per request and your expected request volume, you can estimate monthly usage with a simple formula: requests multiplied by average input tokens and output tokens, priced according to the selected model.

Tarifs · Docs

Prêt à commencer ?

Connectez Claude et GPT en quelques minutes aux prix les plus bas, sans bridage. Inscrivez-vous pour votre clé API.

Inscription gratuite