Enterprise DNA

Omni by Enterprise DNA

Enterprise DNA Resources

Step-by-step how-tos. Practical AI operating-system thinking for owners, operators, and teams doing real work.

220k+

Data professionals

Omni

AI agents and apps

Audit

Map the manual work

Guide Intermediate General

LLM Token Management: Reduce Costs and Improve Performance

A practical walkthrough of LLM token management covering counting, caching, prompt optimization, and cost reduction techniques for production AI systems.

Sam McKay |
LLM Token Management: Reduce Costs and Improve Performance

What Token Management Actually Is

Strip away the marketing and token management is the discipline of measuring, budgeting, and optimizing the units of text that large language models process. Every API call you make to OpenAI, Anthropic, Google, or any other provider charges you based on tokens, not characters or words. A token is roughly four characters of English text, though the exact mapping depends on the model’s tokenizer.

The reason this matters is straightforward. A single careless prompt can cost ten times more than a tight one. A chat application that streams thousands of conversations per day can quietly burn through a budget that looked reasonable in a spreadsheet. Token management is the set of practices that keep that spend predictable and the latency acceptable.

Technically, token management involves four activities. First, counting tokens before sending requests so you can estimate cost and reject oversized inputs. Second, structuring prompts so the model gets what it needs without redundant context. Third, caching repeated content so you don’t pay to process the same system prompt on every call. Fourth, choosing the right model tier for the job rather than defaulting to the most expensive one.

Most teams treat this as an afterthought. They wire up an API call, ship the feature, and discover the bill three months later. The teams that handle this well treat token management as a first-class engineering concern with the same rigor as database query optimization.

Setup and Authentication

You do not install token management as a separate product. It is a layer you build on top of whichever LLM API you already use. The setup steps depend on your provider, but the pattern is consistent.

Start by getting your API key into a secure environment variable. For OpenAI this looks like exporting OPENAI_API_KEY in your shell or storing it in a secrets manager. For Anthropic the variable is ANTHROPIC_API_KEY. For Google it is GOOGLE_API_KEY or a service account JSON file depending on the path you take.

Next, install the official SDK for your provider. The Python examples below use the OpenAI client, but the same shape applies elsewhere.

The pip command is straightforward. Run pip install openai tiktoken in your project environment. The tiktoken package is the tokenizer library OpenAI released, and it gives you accurate counts without making a network call. Anthropic ships a similar tool through its SDK, and there are open source alternatives like transformers tokenizers for Hugging Face models.

Once installed, verify your setup by importing both libraries and printing a count for a sample string. If you see a number, you are ready to proceed. If you see an authentication error, your key is not being read correctly and you should check the environment variable name and the file that loads it.

For production deployments, rotate keys on a schedule and never commit them to source control. Most teams use a managed secrets store such as AWS Secrets Manager, HashiCorp Vault, or the equivalent from their cloud provider. The setup cost is small and the failure mode of leaked keys is severe.

First Working Example

Here is a runnable Python example that counts tokens, estimates cost, and sends a request with a budget check in place.

The script imports openai and tiktoken, defines a function that counts tokens for a given string using the cl100k_base encoding, and then wraps an API call in a guard that refuses to send anything over a configurable threshold. The threshold is in tokens, not dollars, because tokens are the unit you can actually measure before the request goes out.

A typical implementation looks like this in plain language. Load the encoding once at module level so you do not pay the initialization cost on every call. Define a counter function that takes a string and returns an integer. Define a budget function that takes a prompt and a maximum token count and returns either the prompt or raises an exception. Then in your main flow, run the budget check before calling the client.

When you run this against a real prompt, you will see two numbers. The first is the token count of your input. The second is the token count reported by the API in its usage field. These should match closely for the input side. If they diverge significantly, your encoding choice is wrong for the model you are calling.

The cost calculation is a simple multiplication. Take input tokens times the input price per million tokens, add output tokens times the output price per million tokens, and you have the dollar cost of that single request. For current OpenAI pricing, input is typically a few dollars per million tokens and output is several times higher. Anthropic and Google follow similar tiered pricing.

Run the script with a 200-word prompt and a request for a 300-word response. The output should show you roughly 250 to 350 input tokens, the API response, and a cost estimate in the cents range. That single number is what most teams never see, and it is the number that changes behavior.

Key Settings That Matter

The dials most people ignore fall into a few categories and they have outsized impact on cost.

Model selection is the single biggest lever. The difference between the cheapest and most expensive model in a provider’s lineup is often a factor of twenty or more. A classification task that does not need reasoning can run on a small model. A coding assistant that needs careful instruction following cannot. Defaulting to the flagship model for every call is the most common waste pattern.

Prompt structure matters more than people expect. A system prompt that repeats itself, includes examples that are not relevant to the current query, or pads instructions with politeness wastes tokens on every call. Tight system prompts that say exactly what the model needs to do, in the order it needs to do it, routinely cut token counts by thirty to fifty percent compared to casually written prompts.

Max output tokens is a setting many people leave at the default maximum. This is risky because the model can keep generating until it hits the cap, and you pay for every token of output. Setting a realistic ceiling based on what you actually need, say 500 tokens for a summary and 1500 for a draft, prevents runaway generation and makes costs predictable.

Temperature and top_p affect output quality and length but not token count directly. However, higher temperature settings tend to produce longer responses on average because the model explores more possibilities before settling. If you need short answers, lower temperature is usually a better choice than prompt engineering alone.

Streaming versus batch response is a latency versus cost trade-off. Streaming does not change token cost, but it changes when you start paying because you can stop generation early if the response goes off track. Batch API endpoints from major providers offer significant discounts, typically in the range of fifty percent, for workloads that do not need real-time responses.

Caching is the underused lever. If your system prompt is two thousand tokens and you send it on every call, you are paying for those tokens every time. Prompt caching, which OpenAI and Anthropic both offer, charges a reduced rate for cached prefix tokens. For applications with long system prompts and high call volume, this can cut input costs by half or more.

Where It Shines

Token management excels in three specific scenarios and the gains are real.

High volume chat applications are the clearest case. If you serve thousands of conversations per day, even small reductions in average tokens per call compound quickly. A team that cuts average input from 1500 tokens to 900 tokens on 50,000 daily calls saves a meaningful fraction of their monthly bill. The tooling pays for itself within the first week.

Long context workflows are another strong fit. Retrieval augmented generation pipelines often stuff large chunks of context into prompts. Counting tokens before the call lets you decide which chunks to include and which to drop. Without this discipline, you pay for irrelevant context on every query and the model performance actually drops because the signal to noise ratio gets worse.

Multi-tenant platforms where different customers have different usage patterns benefit from per-tenant budgets. You can set hard limits per customer, surface usage in a dashboard, and prevent one heavy user from consuming the budget for everyone else. This is the kind of operational hygiene that becomes critical once you cross a few hundred active users.

Where It Fails

Token management is not a silver bullet and there are honest limitations worth naming.

The tokenizers do not perfectly predict cost because pricing models change and discounts vary. Your counter says 1000 tokens but the actual charge depends on which model you hit, whether caching kicked in, and whether you qualified for a batch discount. The estimate is directionally correct but not exact.

Optimization has diminishing returns. Cutting tokens from 2000 to 1000 is a big win. Cutting from 500 to 400 is usually not worth the engineering effort. Teams that chase the last ten percent of efficiency often spend more in engineering time than they save in API costs.

Token counting does not tell you whether your prompt is good. A tight prompt that asks the wrong question will still cost less than a verbose prompt that asks the right one, but the result will be useless. Quality and cost are separate axes and optimizing one without the other produces bad outcomes.

Some workloads are inherently expensive. Summarizing a 100,000 token document requires sending those tokens in. No amount of management reduces the floor cost of the task itself. In these cases the answer is usually a different architecture, such as chunking the document or using a model with a larger context window, rather than token optimization.

Practical Workflow Pattern

Here is how to slot token management into a real work setup without overengineering it.

Start by instrumenting every API call to log input tokens, output tokens, model used, latency, and estimated cost. Most SDKs return a usage object on every response. Capture it, store it, and review it weekly. This single habit catches most cost problems before they become emergencies.

Add a pre-flight budget check on the request path. Before sending any prompt, count its tokens and reject anything that exceeds a configured maximum. The maximum should be set per endpoint based on what the endpoint actually needs. A chat endpoint might cap at 4000 tokens. A summarization endpoint might cap at 50,000.

Review your system prompts quarterly. They tend to grow over time as features get added and edge cases get documented. A prompt that was 500 tokens in version one can quietly become 3000 tokens in version twelve. Trimming them back to what the model actually needs is a routine maintenance task.

Set up alerts on cost anomalies. Most cloud providers and observability tools let you trigger a notification when daily spend crosses a threshold. Configure this for both your overall budget and per-model budgets so you know which workload is responsible when something spikes.

Finally, make model selection a deliberate choice rather than a default. For each new feature, decide which model tier it needs and document the reasoning. Revisit the decision when new models ship because the cost-quality frontier moves quickly and yesterday’s premium model becomes today’s mid-tier option.

The pattern is measure, budget, optimize, repeat. None of the steps are exotic. The discipline is in doing them consistently rather than treating token management as a one-time project.

Enterprise DNA put together a free field guide on exactly this: the full Claude ecosystem, Claude Code, and how to roll agents out without breaking things. Get the guide.