Curate context and compare quality, latency and cost
Choose which files and examples go into a prompt and compare quality, latency and token cost across prompt revisions.
By Invicly Tech, , in Prompt Engineering and Vibe Coding. About 9 minutes to read.
In short
A model can only read so much at once, and every token costs time and money. The context budget is the choice of which files, examples and notes go into the prompt. You compare prompt revisions on quality, latency and cost together, because the cheapest prompt is not always the best one.
Think of it like this: Packing a suitcase with a weight limit: you take what you will use, and leave the rest at home.
Explained simply
A model reads your prompt as a list of tokens. A token is a small piece of text, often a short word or part of a longer one. Every model has a limit on how many tokens it can read at once, called the context window. You also pay per token, and more tokens usually mean a slower answer.
So the question is not 'how much can I fit?' but 'what does the model need?'. Choose the files, examples and notes that help with this task. Leave out the rest. Irrelevant text can pull the answer off course as well as cost money.
Order matters too. For long documents, Claude's prompting guide suggests putting the documents first and your question at the end. Keep the parts that never change at the start of the prompt, because providers can reuse work for a repeated prefix. This is called prompt caching.
Whenever you change a prompt, measure three things on the same test inputs: quality, latency and cost. A longer prompt that is only slightly better, but twice as slow and costly, may not be worth it.
Press Play on a diagram to walk through it one step at a time.
- 1Pick context
Choose files and examples on purpose
- 2Count tokens
Measure size of each revision
- 3Run the fixtures
Same inputs every time
- 4Compare
Quality, latency and cost side by side
- Back to step 1
- 1System instructions
Stable, good for caching
- 2Long documents
Placed near the top
- 3Examples
Stable set of worked cases
- 4The question or task
Last, after the context
- Input tokens
- Instructions
- Examples
- Documents
- The question
- Output tokens
- The answer
- Any extra explanation
- Cached input
- Repeated stable prefix
- Cheaper or faster when supported
In depth
Choosing context on purpose
Start with the smallest context that could work, then add. For a coding or document task, ask what a person would need to open: the function and its tests, not the whole repository. For examples, keep the few that teach something different.
Write down why each item is included. If you cannot say why, remove it and see if the answer changes. This habit stops a prompt growing every time something goes wrong.
- Smallest context that could work
- One reason per included file or example
- Remove an item and test the difference
- Summarise very long background text
Ordering long context
With long inputs, place the documents near the top and the question or task at the end. Claude's guidance says queries at the end can improve answers on complex, multi-document input. Use tags to separate documents and label each with a source name.
For very long context, ask the model to first pull out the relevant quotes, then answer from them. This helps it focus on the right parts. Always test with your own data, since results vary by model.
- Step 1Stable instructions
Same every call
- Step 2Documents in tags
Each labelled by source
- Step 3Ask for quotes
Relevant parts first
- Step 4Question last
After the context
Counting tokens and using caching
Your provider can report token counts in each response, and many offer a counting tool or tokenizer library. Record the input and output tokens for each revision. Output tokens often cost more than input tokens, so a verbose answer adds up.
Caching works on the start of the prompt. OpenAI applies it automatically to prompts of 1,024 tokens or more when the beginning matches exactly. Anthropic asks you to mark cache points in the request. Either way, put stable text first and variable text last, and check the docs for the current minimum sizes, lifetime and prices.
- Record input and output tokens per revision
- Stable prefix first, changing text last
- An exact prefix match is needed for a cache hit
- Check current cache rules in provider docs
Comparing quality, latency and cost together
Make a small table: one row per prompt revision, columns for a quality score from your checklist, median latency and cost per call. Run each revision on the same fixtures, more than once if timing matters, because latency varies.
Then decide with a rule you set before looking: for example, accept a revision if quality does not drop and cost falls, or if quality rises by at least two checks out of ten. Without a rule it is easy to pick the version you like.
- 900 input tokens
- Median 1.2 s
- Quality 7 of 10 checks
- 2,400 input tokens
- Median 2.0 s
- Quality 9 of 10 checks
Learn it in 5 short steps
About 30m each. Track them with a timer in your plan.
- 1Pick files and examples on purpose~30m
- 2Order long context for recall~30m
- 3Count tokens per revision~30m
- 4Use caching for stable prefixes~30m
- 5Compare quality, latency and cost~30m
Key points
- Choose context on purpose: the files and examples the task really needs, not everything nearby.
- Put long documents first and the question last. Claude's prompting guide notes this helps with long inputs.
- Count tokens for each revision, so a longer prompt has to earn its extra cost.
- Keep the stable part of the prompt at the start. Providers can cache an exact matching prefix, which reduces cost and latency.
- Record quality, latency and cost together for every revision, on the same fixtures.
Key terms
Tap a card to flip it. 0/5 known
Examples
Trimming a prompt without losing quality
A support prompt includes the whole 12-page policy, 8 examples and 3 old tickets. Step 1: count tokens, say 9,000. Step 2: run 10 test tickets and score the answers, 9 of 10 pass. Step 3: replace the policy with the three relevant pages and keep 4 examples. Step 4: count again, 3,500 tokens. Step 5: rerun the 10 tickets. If still 9 of 10 pass, keep the shorter version and note the saving.
Recording a comparison
Keep a simple log file so the decision is easy to review. Each row names the revision, the number of fixtures run and the measured values. The code below writes one row after each run.
import { appendFileSync } from "node:fs";
export function logRun(rev: string, passed: number, total: number,
medianMs: number, inTokens: number, outTokens: number) {
const row = [rev, `${passed}/${total}`, medianMs, inTokens, outTokens].join(",");
appendFileSync("prompt-runs.csv", row + "\n");
}Quick reference
- Tokens are the unit of limits, cost and often speed
- Include only context you can explain, and test removing it
- Documents first, question last for long inputs
- Stable text at the start so a prefix can be cached
- OpenAI caches prefixes of 1,024 or more tokens automatically
- Record input tokens, output tokens, latency and quality
- Set the accept rule before you look at the results
Common mistakes
- Adding more context after every failure, until the prompt is slow and the real instruction is buried.
- Putting a changing value, such as the time, at the top and breaking the cached prefix.
- Judging a revision on one run instead of the whole fixture set.
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Take a prompt that includes a long document. List which parts the task really needs and which could be cut.
- Exercise 2medium
Create two revisions of a prompt, run both on 5 inputs, and record tokens, time and a pass count in a table.
- Exercise 3hard
Restructure a prompt so the stable parts come first. Explain how you would check whether caching helped.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- Is a bigger context window always better?
- No. A bigger window lets you send more, but more text costs more, takes longer and can distract the model. Send what is needed.
- How do I count tokens?
- Use your provider's token counting tool or read the usage numbers in the response. Counts differ between models, so count with the model you use.
- Does caching change the answer?
- It is meant to save cost and time without changing the answer, but check the provider's docs for exact behaviour and limits.