Trim to Tokens
AI / AgentTruncate 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.
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
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 →