What to Do When Your System Prompt Balloons to 200,000 Characters
Learn why LLM system prompts bloat, how to fix it with layered context loading and on-demand retrieval, and how to enforce evaluation gates, cost attribution, and privacy boundaries. Includes a minimal Python example and practical thresholds.
Why Prompts Bloat
Start with the conclusion: prompt bloat is usually not caused by verbose writers, but by missing layering and missing gates. One publicly disclosed case (unverified) reportedly had a general-purpose assistant whose single system prompt version approached 200,000 characters, up from roughly 120,000 in the previous version. Note that the source of these numbers cannot be verified, and the counting method is not stated (whether spaces, line breaks, and multimodal markers are included). Treat them only as a “possible” signal, not as a general fact.
The key question: when a prompt grows by tens of thousands of characters in a month, the team should first ask “what content must stay resident at all times,” instead of continuing to append paragraphs.
The Real Drivers of Bloat
- Rule patching: every production issue is solved by adding another rule block.
- Memory file bloat: user profiles, preferences, and past conclusions are all stuffed in at full length.
- No versioning or rollback: there is no way to tell which block actually delivered value.
- No cost attribution: nobody knows how many tokens or how much money each layer consumes.
How to Do Layered Loading and On-Demand Retrieval
Core principle: inject only the minimum context required for the current decision on each turn, and leave the full body text to a retrieval interface that reads on demand.
| Layer | Content | Injection timing | Size cap (suggested) |
|---|---|---|---|
| L0 Identity and safety | Role, compliance red lines | Every turn | 800 characters |
| L1 Task instructions | Steps for the current task | Every turn | 1,500 characters |
| L2 Memory summary | User profile/preference summary | Every turn | 1,200 characters |
| L3 Memory index | File name + tags + last updated | Every turn | 600 characters |
| L4 Memory body | Full text of a specific entry | Retrieved on hit | 3,000 characters per entry |
On-demand retrieval must cover its prerequisites, otherwise the flow breaks:
- Retrieval interface: input query, top_k, time range; output entries, score, source path.
- Index permissions: isolated per user/tenant, with unauthorized reads denied by default.
- Timeout and fallback: if retrieval exceeds 300ms, fall back to the L2 summary and mark “information may be incomplete.”
- Cache and consistency: cache summaries for 5 minutes; invalidate related caches after writes to avoid reading stale memory.
Sample code (minimal and runnable, Python 3.10+, no third-party dependencies):
def build_context(user_id, query, read_memory):base = "L0+L1+L2+L3 already injected"try:items = read_memory(user_id, query, top_k=3, timeout_ms=300)return base + "\n" + "\n".join(i.text[:3000] for i in items)except Exception:return base + "\n[fallback] Memory retrieval failed, using summary"
How to Set Up an Evaluation Set and Regression Gates
Without gates, governance is just a slogan. A minimal evaluation set: 200 real task samples covering high-frequency cases, long-tail cases, unauthorized access, and empty results.
- Metrics: task success rate, P95 latency, cost per call.
- Gate thresholds: success rate no lower than the previous version minus 2%; P95 latency no more than +150ms; cost no more than +5%.
- Rollback flow: if a gate fails, automatically roll back to the previous prompt version and keep the diff plus the failing samples.
Observability and Cost Attribution
Instrumentation fields: session_id, prompt_version, layer (L0-L4), tokens_by_layer, retrieval_count, cache_hit, latency_ms, cost.
Attribution dimensions: by layer, by session, by retrieval count. Only then can you answer “where exactly is the bloat expensive.”
Privacy Boundaries and Conflict Handling
- Data minimization: store only the fields required to complete the task.
- Masking: mask phone numbers, addresses, and ID numbers before storage.
- Retention: memory entries expire after 90 days by default, configurable.
- Risk note: on-demand reads carry the risk of unauthorized access and data loss or leakage, so permission checks and audit logs are mandatory.
- Conflict handling: when new memory conflicts with old memory, keep both versions marked with timestamps, and let the task policy decide priority.
Common Mistakes
- Full injection: stuffing memory bodies into every turn, so latency and cost spiral out of control.
- No version rollback: prompt changes are made on gut feeling, and there is no way to recover when something breaks.
- No cost attribution: you only know the bill went up, not which layer drove it.
How to Choose an Approach
If the team has fewer than 5 people: start with L0-L3 layering plus a 200-sample evaluation set. If a retrieval system already exists: prioritize permissions, timeout fallback, and instrumentation. If compliance requirements are strict: first add masking, retention periods, and auditing, then talk about bloat governance.
