---
title: "What is a token, and why does my prompt cost more than I expected?"
description: "Tokens are the chunks a model reads and writes, not words. Code and JSON tokenise badly, output costs more than input, and your history is resent every turn."
url: https://www.danny.engineering/answer/ai/what-is-a-token-and-why-does-my-prompt-cost-more-than-i-expected
author: Danny
published: 2026-12-03T00:00:00.000Z
updated: 2026-12-03T15:34:00.000Z
tags: ["AI"]
---

# What is a token, and why does my prompt cost more than I expected?

## The short answer


A token is the unit a language model actually reads and writes. It is not a word and not a character: it is a chunk of text, often a whole short word, sometimes a fragment of one, sometimes a single bracket or space. In English prose one token is roughly four characters, or about three quarters of a word. You are billed per token, input and output are priced differently, and everything you send, including the system prompt and the whole conversation so far, is counted again on every request. Those three facts explain almost every surprising bill.


```typescript
import Anthropic from '@anthropic-ai/sdk';

const client = new Anthropic();

const response = await client.messages.create({
  model: 'claude-opus-5-5',
  max_tokens: 1024,
  messages: [{ role: 'user', content: 'Explain closures in one paragraph.' }],
});

console.log(response.usage);
// e.g. { input_tokens: 14, output_tokens: 180, ... }
```


## How it works


Before a model sees your text, a tokeniser splits it into pieces drawn from a fixed vocabulary of tens of thousands of entries. Common English words such as "the" or "function" are usually one token each. Rarer words are split: "unbelievable" might become "un", "believ" and "able". Numbers, punctuation and whitespace all take tokens too. Each provider trains its own tokeniser, so the same string can have a different count on different models, and a count from one provider's tool tells you little about another's.


**Tokens versus words versus characters.** The four-characters rule is a rough average over ordinary English. It is good enough for a back-of-envelope estimate and not good enough for a budget. The only accurate count is the one the model's own tokeniser gives you.


**Why code and JSON cost more.** Code is dense with symbols. Every brace, quote, colon and comma is usually its own token, and so is each level of indentation. Identifiers such as `getUserPreferences` split into several pieces, and UUIDs, hashes and base64 strings tokenise close to one token per character or two. Pretty-printed JSON spends tokens on whitespace that carries no meaning. A 2 KB JSON payload can easily cost twice as many tokens as 2 KB of prose. Text in languages other than English also tends to cost more per word.


**Input versus output pricing.** Output tokens cost far more than input tokens because generating is more expensive than reading. On `claude-opus-5-5` input is $4 per million tokens and output is $20 per million, five times as much. A short question that produces a long answer is dominated by output cost. On models that reason before answering, that reasoning is billed as output tokens as well, so the `effort` setting affects the bill too.


**History is resent every turn.** The API is stateless. Each request must carry the system prompt, every earlier user and assistant message, and the new message. Turn ten of a chat is not ten messages' worth of input, it is the sum of all ten turns plus the system prompt. Costs grow roughly with the square of the conversation length if you never trim.


## How to see and reduce the cost


### Count before you send


`countTokens` runs the real tokeniser for the model you name and returns the input count without generating anything. Use it to size a request before you commit to it, and to compare two versions of a prompt. Do not use `tiktoken` or similar libraries for Claude: they implement another provider's tokeniser and undercount, especially on code.


```typescript
const { input_tokens } = await client.messages.countTokens({
  model: 'claude-opus-5-5',
  system: systemPrompt,
  messages,
});

console.log(`This request will send ${input_tokens} input tokens`);
```


### Read usage on every response


Every response carries a `usage` object. Log it. Over a day of real traffic it tells you where the money goes far better than any estimate.


```typescript
const { input_tokens, output_tokens, cache_read_input_tokens } = response.usage;
const cost = (input_tokens * 4 + output_tokens * 20) / 1_000_000;
console.log({ input_tokens, output_tokens, cache_read_input_tokens, cost });
```


### Trim what you resend


Keep the system prompt as short as it can be while still doing its job. Drop or summarise old turns once a conversation gets long, and never resend a tool result or a document that the model no longer needs. If the stable part of your prompt is large, prompt caching makes repeated reads of it cost about a tenth of the normal input price.


```typescript
const response = await client.messages.create({
  model: 'claude-opus-5-5',
  max_tokens: 1024,
  system: [
    { type: 'text', text: bigStableInstructions, cache_control: { type: 'ephemeral' } },
  ],
  messages,
});
```


### Send compact data


Minify JSON before you send it, strip fields the model does not need, and prefer a plain list over a nested object when the structure does not matter. For code, send the relevant function rather than the whole file.


```typescript
const compact = JSON.stringify(order); // not JSON.stringify(order, null, 2)
```


### Cap and shape the output


`max_tokens` is a hard ceiling, so set it to what the task needs rather than the maximum. More useful still is asking for less: "answer in two sentences" or a structured output with a tight schema reduces output tokens directly, and output is the expensive side.


## Common pitfalls

- **Estimating with another provider's tokeniser.** Counts differ between models. Use `countTokens` for Claude, and your provider's equivalent elsewhere.
- **Forgetting the system prompt is resent.** A 3,000-token system prompt costs 3,000 input tokens on every single call, not once.
- **Pretty-printing JSON into the prompt.** Indentation and newlines are tokens. Minify, or convert to a compact text format.
- **Treating** **`max_tokens`** **as a cost control.** It is a cap, not a target. Hitting it truncates the answer mid-sentence and you often end up paying for a retry.
- **Ignoring the output side.** Input is the number people look at, but a verbose answer at five times the price is usually what inflates the bill.
- **Dumping a whole file when a slice would do.** The model does not need the imports and the licence header to review one function.

## Related reading

- [Token counting](https://platform.claude.com/docs/en/build-with-claude/token-counting)
- [Pricing](https://platform.claude.com/docs/en/about-claude/pricing)
- [Prompt caching](https://platform.claude.com/docs/en/build-with-claude/prompt-caching)
