DEV Community

Dwelvin Morgan
Dwelvin Morgan

Posted on

Cutting LLM API costs by 40 percent with context detection

#ai

Cutting LLM API costs by 40 percent with context detection

The Problem

Every prompt you send to an LLM costs money, and most prompts are longer than they need to be. I was spending $200-300/month on API calls where at least 30% of the tokens were filler context, redundant phrasing, or instructions the model didn't need to follow my intent. The problem isn't just verbosity — it's that generic prompt optimization treats every request the same way, so you either over-optimize and lose critical context, or under-optimize and waste tokens on every call.

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. I measured 91.94% accuracy on context detection across all categories, which means the system correctly identifies your intent 9 times out of 10 without any fine-tuning or training data from your prompts. For image and video generation tasks, accuracy jumps to 96.4% because those requests have distinct structural patterns the classifier picks up immediately.

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."
Enter fullscreen mode Exit fullscreen mode

The optimized version is 40% shorter but 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. No tokens wasted on filler phrases like "I need you to" or "make sure it handles" — the model gets the instruction in the most direct form.

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

The "AHA" Moment

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. That's fine for most use cases, but it means you won't see the full cost reduction on very short prompts.

What I measured

I measured a 40% reduction in API costs on my own usage after routing prompts through context detection. The cost difference comes from two sources: shorter prompts (average 35% fewer tokens per request) and fewer retry calls (when the optimized prompt is more precise, I don't need to re-run with clarifications). I tracked this over 300 requests across code generation, analysis, and creative tasks. The biggest savings came from code generation prompts, where the original versions averaged 180 tokens and the optimized versions averaged 95 tokens — a 47% reduction.

Accuracy held steady at 91.94% overall, with the highest performance on image and video generation tasks (96.4%). Those categories have the most distinct structural patterns — output format requests, aspect ratio constraints, style descriptors — so the classifier rarely misses. The lowest accuracy was on general_task prompts (85.2%), 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.

One unexpected result: the optimization quality improved my prompt writing over time. After seeing how the system restructured my prompts — frontloading constraints, removing filler, making output expectations explicit — I started writing tighter first drafts. I still run everything through the optimizer, but my pre-optimized prompts are now 20% shorter than they were three months ago. The cost savings compound when your baseline prompts are already closer to optimal structure.

Key Takeaways

  • Context detection pays for itself in token savings — 40% cost reduction on my own usage, measured over 300 requests. The ROI is immediate if you're running more than 50 API calls per week.
  • Accuracy matters more than speed for optimization. A 91.94% correct classification rate means you can trust the system to preserve your intent without manual review on 9 out of 10 prompts. The 10th one 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.
  • Short prompts don't benefit as much. If your average prompt is under 20 tokens, context detection won't have enough signal to classify reliably. The system falls back to minimal optimization, which still helps but won't hit the 40% cost reduction.
  • Use explore_sop_approaches when 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 91.94% context detection accuracy.

Top comments (0)