惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

人人都是产品经理
人人都是产品经理
Stack Overflow Blog
Stack Overflow Blog
S
SegmentFault 最新的问题
博客园 - 司徒正美
aimingoo的专栏
aimingoo的专栏
U
Unit 42
GbyAI
GbyAI
B
Blog RSS Feed
博客园 - Franky
L
LangChain Blog
Hugging Face - Blog
Hugging Face - Blog
美团技术团队
The GitHub Blog
The GitHub Blog
Y
Y Combinator Blog
云风的 BLOG
云风的 BLOG
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园 - 三生石上(FineUI控件)
Microsoft Azure Blog
Microsoft Azure Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
G
Google Developers Blog
Last Week in AI
Last Week in AI
阮一峰的网络日志
阮一峰的网络日志
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Apple Machine Learning Research
Apple Machine Learning Research

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
How LLM Tokens Work (And Why They Explain Your AI Bill)
thestackunderflow · 2026-06-24 · via DEV Community

thestackunderflow

Your LLM never reads your words — it reads tokens. And almost every surprise on your AI bill traces back to that one fact. Here's the breakdown 👇

Here's the thing almost nobody internalizes about large language models: Claude never reads your words. It reads tokens — numbers. Your prompt is chopped into pieces, each piece is mapped to an integer, and the model only ever sees those integers. Every limit you hit, every bill you pay, and half the weird behavior you've seen traces back to this one fact.

This article explains what a token actually is, why the model works in tokens instead of words, and how that single design choice explains your AI bill.

The one-sentence version: text is split into tokens (chunks roughly ¾ of a word on average), each token maps to a number, and you pay per token — in and out — so understanding tokens is understanding cost.

What a token actually is

A token is a chunk of text — often a word, but frequently a piece of a word, a space, or a punctuation mark. The tokenizer is a fixed dictionary that maps text chunks to integer IDs.

Rough intuition:

  • Common words (the, code, error) are usually one token.
  • Rare or long words split into several tokens (tokenizationtoken + ization).
  • Whitespace and punctuation are tokens too.
  • A useful rule of thumb in English: ~4 characters per token, or ~0.75 words per token.

So "How tokens work" isn't 3 words to the model — it's a sequence of integer IDs like [4438, 11460, 990]. The model does math on those numbers. The English you typed was never seen.

Why models use tokens instead of words or letters

Two extremes, both bad:

  • Whole words: the vocabulary would be enormous and would break on any word it had never seen.
  • Single characters: sequences would be absurdly long and the model would waste capacity relearning how letters form words.

Tokens are the engineered middle: a fixed vocabulary (tens of thousands of entries) of common chunks that can assemble any text — including words the model has never encountered — by gluing pieces together. It's the compression that makes the whole thing tractable.

Why this explains your bill

Every API provider, including Anthropic, prices per token — and counts both directions:

  • Input tokens: everything you send — your prompt, the system prompt, the conversation history, retrieved documents, tool definitions. All of it.
  • Output tokens: everything the model generates back. Output is typically priced several times higher than input.

This is why costs surprise people:

  • Long context isn't free. If you stuff 50 pages into the prompt "just in case," you pay for all of it on every call.
  • Conversation history compounds. In a chat, each new turn resends the whole prior conversation as input. Turn 20 is paying for turns 1–19 again.
  • Verbose output costs more than verbose input. Asking for a 2,000-word answer is pricier than sending a 2,000-word prompt.
Your bill ≈ (input tokens × input price) + (output tokens × output price)
            └── prompt + history + docs + tools      └── the model's reply

A worked intuition

Say input is priced at $3 per million tokens and output at $15 per million (illustrative — check current rates). You send a 1,000-token prompt and get a 500-token answer:

  • Input: 1,000 × ($3 / 1,000,000) = $0.003
  • Output: 500 × ($15 / 1,000,000) = $0.0075
  • One call ≈ $0.01

Tiny — until you multiply by thousands of calls, or let conversation history balloon each call's input to 20,000 tokens. That's where bills come from: not one expensive call, but token count × call count.

How to reason about token cost

  • Count tokens, not words, when estimating cost.
  • Trim the prompt to what's needed. Every "just in case" paragraph is paid for on every call.
  • Watch history growth in chat apps — prune or summarize old turns.
  • Constrain output length when you don't need an essay.
  • Cache the stable prefix if you reuse the same big context repeatedly.

Common misconceptions

  • "The model reads my text." No — it reads token IDs. Your words are converted first.
  • "One word = one token." Often, but long/rare words split into multiple tokens, and spaces/punctuation count.
  • "Only output costs money." Both directions are billed; input is usually cheaper per token but there's a lot more of it.
  • "A bigger context window is free to use." The capacity is available; using it costs tokens on every call.

Frequently asked questions

How many tokens is a typical page of text?
Roughly 500–800 tokens per page of prose, but it varies with formatting and vocabulary.

Why do code and JSON sometimes cost more tokens than they look?
Symbols, indentation, and braces each tokenize separately, so structured text can be token-dense relative to its character count.

Does the system prompt count?
Yes. The system prompt, tool definitions, and any retrieved context are all input tokens you pay for on every call.

Is there a way to avoid resending the same big context every time?
Yes — prompt caching lets you reuse a stable prefix at a fraction of the cost.


I'm doing a whole series taking Claude apart piece by piece — video + written version of each — at The Stack Underflow. The full written companion to this one, plus the rest of the series, lives at thestackunderflow.com/tutorials.