惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Docker
Apple Machine Learning Research
Apple Machine Learning Research
宝玉的分享
宝玉的分享
博客园 - 叶小钗
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 司徒正美
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - Franky
爱范儿
爱范儿
罗磊的独立博客
IT之家
IT之家
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
N
Netflix TechBlog - Medium
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
U
Unit 42
Engineering at Meta
Engineering at Meta
WordPress大学
WordPress大学
博客园 - 三生石上(FineUI控件)
T
Tailwind CSS Blog
H
Help Net Security
博客园_首页
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
人人都是产品经理
人人都是产品经理

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Fix Your Prompt Structure Before You Touch Your Infrastru...
Parag Darade · 2026-04-30 · via DEV Community

Parag Darade

Fix Your Prompt Structure Before You Touch Your Infrastructure

Most engineering teams treat LLM inference costs as an infrastructure problem. They evaluate model quantization, shop for cheaper GPU rentals, debate whether to move from GPT-4o to Claude Sonnet, and benchmark open-source alternatives. I have watched teams spend weeks on this and save fifteen percent. The same teams were running their system prompts with a timestamp in the first line and paying full token price on every single request.

The optimization I am talking about is prompt caching. Anthropic charges $0.30 per million tokens for cache reads versus $3.00 per million for fresh input tokens — a 10x price difference for bytes the model already processed in the last hour. OpenAI applies automatic 50% discounts on cached tokens. The savings are not theoretical. They compound over every request your system makes, and most teams are not capturing them because they are breaking the cache themselves.

What cache-busting actually looks like

Caching works by hashing the prefix of your prompt. If the hash matches a recent request, you pay the cheap rate. If it does not, you pay full price on the entire prefix.

The failure mode is deceptively simple: any dynamic content in your system prompt breaks the hash. A current timestamp. A user ID. A session context block. A "today's date is April 30, 2026" string you added because the model kept getting dates wrong. Any of these changes between requests, which pushes the cache hit rate to near zero and guarantees you pay $3.00/M on every input token.

I have seen this specific mistake in every LLM system I have audited that started as a quick prototype. The system prompt grows organically — someone adds a date, someone adds a user's account tier, someone adds a "recent conversation summary" block — and by the time the system is in production, the cacheable prefix is maybe the first hundred tokens of a twenty-thousand-token prompt. Cache hit rate: seven percent.

The ProjectDiscovery case

ProjectDiscovery's engineering team published a detailed breakdown of exactly this problem in early 2025. Their security agent Neo runs an average of 26 steps per task with roughly 40 tool calls. Each step sent a prompt that included a 20,000-token system prompt — 2,500 lines of YAML, tool definitions, and runtime state including working memory and skills context that changed every step.

Their initial cache hit rate: 7%.

The fix was structural. They moved dynamic content — working memory, runtime variables, skills context — out of the system prompt and into a user message appended at the tail of the conversation. The static system prompt stayed static. The dynamic state moved to the only place it should have been: after the stable prefix.

Cache hit rate after the change: 74% within the same deployment cycle, 84% by mid-March 2025. Total cost reduction: 59% compared to baseline. Over the six weeks following deployment, they served 9.8 billion input tokens from cache rather than paying full price for them.

That last number is worth sitting with. 9.8 billion tokens at $0.30/M instead of $3.00/M. The engineering work took days.

The structural rule

Everything static goes first. Everything dynamic goes last.

Your system prompt — instructions, persona, output format, tool definitions — is static. It changes when you ship a new version, not on every request. Mark it as cacheable and never mix runtime state into it. Your dynamic content — user context, current date, session variables, retrieved chunks from RAG — goes into the user message at the end of the conversation. This is where the model expects context to live anyway.

Tool definitions deserve a specific note. If your tool list is partly static (the core tools your agent always has) and partly dynamic (tools you inject based on user permissions or task context), sort the static tools first and place them before the dynamic ones. ProjectDiscovery made tool definitions their second cache breakpoint, keeping a 1-hour TTL on the stable portion even in conversations with changing tool sets. The incremental token cost of alphabetically sorting a list of tool names before your agent runs is zero. The cache savings compound over every task.

How to tell if you are affected

Pull your Anthropic usage metrics for the last seven days. If your cache read token rate is below 40% of total input tokens, your prompts are almost certainly structured wrong. Below 20% means something dynamic is almost certainly in the system prompt itself — probably a date, a user attribute, or a context block that varies per session.

For OpenAI, automatic caching applies the 50% discount whenever a matching prefix exists, so the failure mode is less visible in billing. You are still missing cache hits, you just do not see the rate directly in your dashboard without explicitly measuring prefix stability.

Where this sits in the optimization stack

Before re-ranking. Before switching embedding models. Before evaluating managed vector databases. Before any model fine-tuning. The ZenML survey of 1,200 production LLM deployments cites Care Access achieving 86% cost reduction through prompt caching, and Riskspan cutting per-deal processing costs by 90x through LLM optimization. Both numbers are large enough to sound inflated, but the mechanism is reliable: if you move from a 7% cache hit rate to a 74% cache hit rate on a 20,000-token prompt, you have changed the effective price of those input tokens by roughly 6x.

The audit your system needs first is not an architecture review. It is reading your own system prompt and asking which lines change between requests. Move those lines to the bottom. The savings will show up in your billing dashboard before the end of the week.