Sub topic 6
Trim the prompt
This chapter assumes you can call an LLM API, know what system messages and tokens are, and have read the earlier chapters on token pricing and on building a baseline. Input tokens are billed on every request, and a longer prompt also lengthens prefill, which is the stage that delays the first token. Trimming the prompt cuts both cost and latency.
Find what is in your prompt
Start from your baseline. Split each prompt into parts and count the tokens in each one. Typical parts are the system message, instructions, examples, retrieved context, conversation history, and the user's question. You can't trim what you haven't measured.
import tiktoken
enc = tiktoken.get_encoding("cl100k_base")
def token_breakdown(parts: dict) -> dict:
return {name: len(enc.encode(text)) for name, text in parts.items()}
parts = {
"system": "You are a support assistant for Acme. Be concise and polite.",
"examples": "Q: Where is my order?\nA: Check the Orders page.\n" * 5,
"history": "User: hi\nAssistant: hello\n" * 20,
"question": "How do I reset my password?",
}
print(token_breakdown(parts))In many applications one or two parts account for most of the tokens. Work on those first.
Common ways to trim
- Cut repeated or filler instructions. Say each rule once. Remove phrases such as "please make sure to carefully" that add no constraint.
- Reduce few-shot examples. Keep the fewest examples that hold quality. Try five, then three, then one, and check each step.
- Limit conversation history. Keep the last few turns, or replace older turns with a short summary.
- Strip formatting noise. Remove HTML tags, repeated whitespace, boilerplate headers, and signatures from documents before inserting them.
- Use compact data formats. Send only the fields the model needs, not whole JSON records with long key names.
Here is a simple history trimmer that keeps the system message and the most recent turns within a token budget:
import tiktoken
enc = tiktoken.get_encoding("cl100k_base")
def count(message: dict) -> int:
return len(enc.encode(message["content"]))
def trim_history(messages: list, max_tokens: int) -> list:
system, turns = messages[0], messages[1:]
kept, used = [], count(system)
for message in reversed(turns):
used += count(message)
if used > max_tokens:
break
kept.append(message)
return [system] + list(reversed(kept))This version ignores the few extra tokens each message adds for role markers. Leave some headroom in max_tokens for that.
Trim one thing at a time
Make one change per run so that any quality drop has an obvious cause. Run your evaluation set after each change, as described in the earlier quality chapter.
Check the result
Compare against the baseline using the same requests. Look at mean input tokens, cost per request, and time to first token. Prefill time scales roughly with input length, so a large cut in tokens should show up in time to first token. Small cuts often disappear into network noise.
Exercise
Your prompt is 3,000 tokens, and 2,000 of them are few-shot examples. You remove all the examples at once, and the evaluation score falls. What should you do differently, and why?
Show answer
Remove the examples gradually, for instance from 10 to 5 to 3 to 1, and run the evaluation set after each step. Changing one thing at a time shows the smallest number of examples that still meets your quality bar. Removing everything at once only tells you that the examples mattered.