All tools

Trim to Tokens

AI / Agent

Truncate text or prompts to a token budget: keep start, end, or middle. Essential for fitting context into LLM windows.

Fitting a long document into a specific model's context window means truncating it to a token budget, and this uses the same real tokenizer libraries as the Token Estimator tool to cut precisely at token boundaries for whichever model you select, rather than guessing at a character count. Three modes cover different cases: keep-start for the common truncation, keep-end when the important part (a conclusion, recent chat messages) is at the bottom, and keep-middle for preserving context on both sides, like trimming a large code file around a specific function. It's worth knowing this is a length cut, not a rewrite. For actually compressing content while preserving meaning, you'd want Summarize for LLM instead, since trimming can occasionally cut mid-word given that tokens don't map 1:1 to words.

tokenstrimtruncatecontextllmbudget

How to use Trim to Tokens

  • 1.Paste a long prompt and set a token budget to truncate it down to fit a specific model's context window.
  • 2.Choose "keep end" mode when the most relevant part of a document (like a conclusion or recent chat messages) is at the bottom.
  • 3.Use "keep middle" mode when you need to preserve context on both sides, such as trimming a large code file around a specific function.

Frequently asked questions

Does this run in my browser or on a server?
Trimming runs entirely client-side using the same tokenizer libraries as the Token Estimator tool — your text isn't sent anywhere.
Does it cut cleanly at word boundaries?
It trims at token boundaries, which usually but not always aligns with word boundaries — expect an occasional partial word at the cut point since tokens don't map 1:1 to words.
Which model's tokenizer does it use for the budget?
You select the target model (GPT-4o, Claude, Gemini, etc.) and it trims based on that model's actual tokenizer, since token counts differ across models for the same text.
Is this the same as summarization?
No — this truncates text to fit a length budget; it doesn't rewrite or compress content. For that, you'd need an LLM summarization step.

Use via API, SDK, or MCP

cURL# Free: 1,000 req/day · Pro: 10,000 req/day
curl -X POST https://api.utilix.tech/v1/tools/trim-to-tokens \
  -H "Authorization: Bearer utx_live_..." \
  -H "Content-Type: application/json" \
  -d '{"text":"This is a long document that needs to be trimmed to fit within a context window.","maxTokens":10,"strategy":"end"}'

Get an API key from your dashboard · Full API docs →