AI Infrastructure
What Context-Aware Prompt Optimization Actually Does to Your Token Count
What Context-Aware Prompt Optimization Actually Does to Your Token Count
The Problem
Generic prompt optimization treats every request the same way, so you either over-optimize and lose critical context, or under-optimize and get a rewrite that doesn't actually help. I built context detection to fix that — but I also ran a real token-count audit against production traffic before writing this post, and it's worth being upfront about what it found: most prompts get longer, not shorter, after optimization. Across a 360-prompt production sample, only 1 prompt had fewer tokens after optimization; the rest expanded, because the optimizer adds structure (persona, constraints, format) rather than trimming filler. The exception is long chat histories and already-verbose, redundant prompts, where trimming can offset the added structure — but that's the minority case, not the default.
What changes for you
I built context detection into the optimization pipeline so the system knows what you're trying to do before it starts rewriting your prompt. When you send a prompt through Prompt Optimizer, it runs a classification pass first: is this a code generation task? A creative writing request? An analysis job? Once it knows the context category, it applies optimization goals specific to that intent — preserving technical precision for code, tightening structure for analysis, keeping voice intact for creative work.
This changes what you can do with prompt optimization. Instead of hoping a generic rewrite improves your prompt, you get a version optimized for the actual task. In internal testing, context detection correctly identifies intent on the large majority of prompts without any fine-tuning or training data from your prompts, and performs best on image and video generation tasks, where requests have distinct structural patterns the classifier picks up immediately. We're re-validating these numbers against the current pipeline before republishing a specific figure.
The optimization happens inside your existing workflow. If you're using Claude Desktop, Cline, Cursor, or any of the 14+ MCP-compatible tools, you install once globally with npm install -g mcp-prompt-optimizer, add your API key to the environment config, and the optimizer shows up as a set of tools in your AI interface. No new UI to learn. No copy-paste between windows. You write your prompt, call the optimization tool, and get back a context-aware rewrite in the same conversation thread.
How it works (brief)
The context detection layer uses six specialized classifiers I call Precision Locks — one per context category. Each lock is a pattern-based detector trained to recognize structural markers in prompts: verb patterns, entity types, constraint phrasing, output format requests. When a prompt comes in, all six locks run in parallel and return confidence scores. The highest-scoring lock wins, and its associated optimization ruleset gets applied.
Here's what that looks like in practice. You run mcp-prompt-optimizer from the command line or call it as an MCP tool in your editor. The system reads your prompt, runs the classification pass, and returns both the detected context and the optimized version. If you're in Claude Desktop, you'd see something like this:
Original: "I need you to write a Python function that takes a list of user objects and returns only the ones where the account is active and the subscription hasn't expired. Make sure it handles edge cases."
Detected context: code_generation (confidence: 0.94)
Optimization applied: technical_precision + constraint_preservation
Optimized: "Write a Python function: filter_active_subscribers(users: List[User]) -> List[User]. Return users where user.is_active == True and user.subscription_expiry > datetime.now(). Handle: empty list, None values, missing attributes."
In this specific example, the optimized version is shorter and preserves every technical requirement from the original — the code_generation lock detected the intent, applied precision rules, and restructured the prompt to frontload constraints and expected behavior, cutting filler phrases like "I need you to" or "make sure it handles". This example isn't representative of the average outcome. Measured across a 360-prompt production sample, even the code_generation category expanded on average — the optimizer more often adds explicit structure (persona, constraints, edge-case handling) than it removes filler. Treat individual before/afters as illustrations of quality, not a token-savings guarantee.
For creative or analysis tasks, the optimization goals shift. A creative_writing prompt keeps voice and tone markers intact while tightening structure. An analysis prompt preserves domain terminology and specified frameworks while removing redundant context. The context detection layer is what makes this possible — without it, you'd need to manually tag every prompt or accept a one-size-fits-all rewrite that misses your actual intent.
Real Metrics
Authentic Metrics from Production:
- evaluation_cost: 0 — free model auto-selected
- context_types: 7
- semantic_score_range: 0.0-1.0
What I found that surprised me
The hardest part was tuning the confidence thresholds for each Precision Lock. Early versions of the classifier were too aggressive — a prompt with mixed intent would get forced into a single category, and the optimization would strip out context that didn't fit the detected pattern. I found that prompts asking for "code that explains itself" or "analysis written for a non-technical audience" were getting misclassified because they had markers from multiple categories. I added a fallback rule: if the top two confidence scores are within 0.15 of each other, the system defaults to the less aggressive optimization ruleset and preserves more of the original phrasing.
Another edge case I didn't expect: prompts with embedded examples. If you include a code snippet or a sample output in your original prompt, the classifier sometimes reads that as the primary intent rather than the instruction wrapping it. I tested a fix where the system strips code blocks and quoted text before running classification, then re-inserts them after optimization. That worked for 80% of cases, but I still see occasional misclassifications when the example is longer than the instruction. Current behavior: if the system detects an embedded example, it flags the optimization as "low confidence" and shows you both the original and optimized versions so you can choose.
I also learned that context detection accuracy drops when prompts are under 20 tokens. Short prompts don't have enough structural markers for the locks to differentiate intent reliably. For those cases, the system skips classification and applies a minimal optimization pass — just redundancy removal, no structural changes.
What I measured
I ran a 360-prompt production audit across all seven context categories, using a real tokenizer to count before/after tokens. The result: 354 of 360 prompts expanded, 5 were roughly unchanged, and 1 got smaller. Mean token change was an increase, not a decrease — across every single context category, including code generation and structured output, the categories most likely to contain filler. The optimizer's job is adding the structure a bare prompt is missing (persona, constraints, success criteria, format), and that structure costs tokens. If you're evaluating this tool for cost reduction, don't — evaluate it for output quality and consistency instead. The one place token count reliably drops is when the input itself is already redundant: long chat histories with repeated context, or prompts padded with unnecessary politeness and hedging.
Accuracy was highest on image and video generation tasks. Those categories have the most distinct structural patterns — output format requests, aspect ratio constraints, style descriptors — so the classifier rarely misses. It was lowest on general_task prompts, which makes sense because that category is the catch-all for requests that don't fit the other five locks. I use general_task as the fallback when no other lock hits a confidence threshold above 0.7.
Key Takeaways
- Context detection improves optimization quality, not token count. Measured on a 360-prompt production run, the optimizer expanded 354 of 360 prompts. Don't adopt this tool expecting API cost savings — adopt it for more reliable, better-structured prompts.
- Accuracy matters more than speed for optimization. A high correct-classification rate means you can trust the system to preserve your intent without manual review on most prompts. The occasional miss you catch in review.
- Install once, use everywhere. MCP-native means the optimizer works in any tool that supports the protocol — Claude Desktop, Cline, Cursor, Windsurf, and 14+ more. No per-tool configuration, no custom integrations.
- The rare case where tokens actually drop: long chat histories and prompts with a lot of redundant, repeated context. If your prompt is already tight, expect it to grow.
- Use
explore_sop_approacheswhen you're starting a new agent workflow. Seeing three different structural strategies side-by-side takes 30 seconds and usually surfaces an approach you wouldn't have written yourself. I use this tool every time I build a new automation.
Want to try it yourself? Try Prompt Optimizer free at https://promptoptimizer.xyz
Building Prompt Optimizer. MCP-native prompt optimization with context-aware detection.