How to Compress Long AI Coding Prompts: Context Slimming and Practical Templates

A practical guide to compressing long AI coding prompts: turn goals, background, constraints, file scope, logs, and project rules into shorter, steadier task cards that coding agents can execute.

After using AI coding tools for a while, many people make prompts longer and longer.

It starts with “help me fix this bug,” then grows into hundreds of words of background, a dozen rules, full error logs, directory notes, historical decisions, personal preferences, test requirements, and a pile of “also.” It looks complete, but it often works worse: the model misses the point, reads too many files, spends more tokens, and drifts from the task.

Prompt compression is not about making every sentence short. It is about removing low-density content so the AI can quickly see the task boundary, input material, constraints, and acceptance criteria.

This guide applies to Codex, Claude Code, Cursor, Gemini CLI, Aider, and similar coding agents.

Quick Answer: Turn Long Prompts Into Task Cards

Most AI coding prompts can be compressed into six lines:

1
2
3
4
5
6
Goal: Fix failed login session refresh.
Scope: Start with src/auth/session.ts and tests/auth/session.test.ts.
Limit: First pass is analysis only; do not modify files; do not scan the whole repo.
Constraints: Do not change public API; do not delete files.
Validation: Give the smallest test command.
Output: List the 3 most likely causes and next steps.

This is steadier than a long paragraph because the model can identify what to do, where to look, what not to do, how to verify, and what to deliver.

The goal is not “write less.” It is “make the model guess less.”

Why Long Prompts Get Worse

Long prompts are not automatically more reliable. Common problems:

1. The Goal Is Buried

Lots of history and opinions may make the model summarize background instead of solving the current task.

2. Constraints Conflict

For example:

  • make the smallest change;
  • refactor while you are there;
  • add full tests;
  • do not change much;
  • architecture changes are allowed;
  • fix it quickly.

The model cannot infer priorities clearly.

3. File Scope Is Too Wide

Words like “the whole project,” “check everything,” and “optimize all related files” encourage the Agent to read too much.

4. Logs and Code Are Pasted in Full

Full logs, full files, and full diffs bury the useful facts.

5. Temporary Needs Become Long-Term Rules

One-off tasks should not be copied into CLAUDE.md, AGENTS.md, or memory. They will pollute later tasks.

If the symptom is a Claude Code token spike, see: /en/2026/07/08/claude-code-token-usage-spike-troubleshooting-faq/. This article focuses on prompt compression itself.

Step 1: Identify What Is Long

Break the prompt into types:

Content type Required? Compression method
Current goal Required Keep one clear verb
File scope Required Write exact paths
Constraints Required Keep constraints that affect actions
Acceptance Required Write test command or checkable result
History Sometimes Keep only conclusions relevant now
Full logs Usually no Keep key error lines
Full code Usually no Give paths and let Agent read
Preferences Sometimes Put long-term preferences in rules
Emotion No Remove
“Also” tasks Avoid Split into later tasks

Ask: will this sentence change the Agent’s next action? If not, delete it or move it to reference material.

Step 2: Convert Natural Language Into Fields

Long prompt:

1
I recently changed the login module. Users report that refreshing the page often logs them out. I suspect session expiry handling or cookie updates. Please look at it, avoid affecting other login logic, don't rewrite the architecture, maybe add tests, and check if similar problems exist elsewhere.

Compressed:

1
2
3
4
5
Goal: Diagnose login state lost after page refresh.
Scope: src/auth/session.ts, src/auth/cookie.ts, tests/auth/session.test.ts.
Limit: Do not change auth architecture; first pass analysis only.
Validation: List minimal reproduction and tests to add.
Output: 3 likely causes ranked by probability.

The information is not reduced. It is more executable.

AI coding prompts should feel like work orders, not essays.

Step 3: Keep Only Background That Changes Decisions

Low-density background:

1
This project had a login refactor before. To support old users we added a compatibility layer. Later the product required mobile and web to behave the same, so this area is historically complex. I remember a cookie issue before but am not sure if it is the same one.

Compressed:

1
2
3
4
Background facts:
- Login has a legacy compatibility layer; do not remove it.
- Web and mobile share session logic.
- There was a previous cookie write issue; check cookie update paths first.

Turn stories into facts and risks.

Step 4: Make “Do Not Do” Concrete

Vague:

1
This area is sensitive, so please be careful and don't change too much unless necessary.

Concrete:

1
2
3
4
5
Forbidden:
- Do not change public API.
- Do not change database schema.
- Do not delete legacy compatibility logic.
- Do not do unrelated refactoring.

Negative constraints should name the actual forbidden actions. For file safety, see: /en/2026/07/10/ai-coding-avoid-accidental-file-deletion/.

Step 5: Remove “Also”

“Also” expands scope:

1
2
3
4
Also optimize the structure.
Also add tests.
Also check for other issues.
Also update docs.

Use phases:

1
2
3
4
Phase 1: Diagnose the bug only; do not modify.
Phase 2: Fix the minimal code path.
Phase 3: Add the smallest related test.
Phase 4: Separately evaluate whether refactoring is needed.

For long work, ask the Agent for a phase plan and execute step by step. Recovery guidance: /en/2026/07/10/ai-agent-long-task-resume-guide/.

Step 6: Do Not Paste Full Files

Coding agents can usually read files themselves.

Instead of pasting full files:

1
2
3
4
5
6
Please read:
- src/auth/session.ts
- src/auth/cookie.ts
- tests/auth/session.test.ts

First pass: summarize relevant logic only. Do not modify.

For large files:

1
Focus only on session refresh, cookie update, and logout branches.

Step 7: Keep Logs to Key Lines

Compress logs into:

1
2
3
4
Command: pnpm test -- auth
Failing test: refresh session keeps user logged in
Key error: Expected status 200, received 401
Stack: tests/auth/session.test.ts:42

If the full log is needed:

1
2
Full log is in .tmp/auth-test.log.
Read only the last 80 lines first.

Step 8: Move Long-Term Rules Out of Prompts

If you repeatedly write:

  • use pnpm;
  • do not edit migrations;
  • run tests after API changes;
  • do not delete files;
  • output Chinese summary;
  • search src/lib for existing helpers first;

put them in:

  • AGENTS.md;
  • CLAUDE.md;
  • .cursor/rules/;
  • docs/ai-workflow.md;
  • a Codex Skill;
  • a Claude Code subagent description.

Then say:

1
2
Follow project AGENTS.md.
Extra limit for this task: do not modify src/db/migrations.

For codebase memory and rule files, see: /en/2026/07/10/ai-agent-codebase-memory-tools-comparison/.

Step 9: Compress CLAUDE.md / AGENTS.md

Rules files can be too long too.

Keep:

  • build commands;
  • test commands;
  • forbidden paths;
  • hard style constraints;
  • security boundaries;
  • commit and validation requirements;
  • project-specific pitfalls.

Remove:

  • outdated history;
  • repeated wording;
  • emotional reminders;
  • long architecture background;
  • generic README content;
  • one-off task records;
  • vague words such as “try to” and “pay attention.”

Rewrite:

1
2
- Search `src/components` before adding UI; prefer existing components.
- Do not create a generic component for a one-page change.

If using tools like caveman to compress context, still review safety rules manually: /en/2026/07/03/caveman-agent-output-compression-guide/.

Step 10: Use Three Templates

Diagnosis Template

1
2
3
4
5
6
Goal:
Scope:
Symptom:
Key error:
Limit: First pass analysis only; do not modify files.
Output: 3 most likely causes + minimal validation steps.

Change Template

1
2
3
4
5
6
Goal:
Allowed files:
Forbidden files:
Implementation requirements:
Validation command:
Output: changed files + validation result + risks.

Review Template

1
2
3
4
Goal: Read-only review current diff.
Focus:
Limit: Do not modify files.
Output: List issues by severity; if none, say no high-risk issue found.

If you use Cursor, Codex, and Claude Code together, assign templates by role: Cursor locates, Claude Code implements, Codex reviews. See: /en/2026/07/10/cursor-codex-claude-code-workflow-division/.

Before and After

Original:

1
I have a login issue where users often lose login state after refreshing. This project changed login several times and has old token compatibility. Cookie handling is complicated. Please look through related code and see whether session expiry or cookie writing is wrong. Avoid affecting other features or architecture. Also add tests and check similar problems. Explain the reason in detail.

Compressed:

1
2
3
4
5
6
Goal: Diagnose login state lost after page refresh.
Scope: src/auth/session.ts, src/auth/cookie.ts, tests/auth/session.test.ts.
Background: There is old token compatibility; do not remove it.
Limit: First pass analysis only; do not modify; do not scan the whole repo.
Validation: Give minimal reproduction and test suggestions.
Output: 3 causes ranked by probability.

Shorter, and clearer.

What Not to Over-Compress

Do not remove:

  • safety boundaries;
  • permission limits;
  • database migration rules;
  • production risk;
  • files that must not be deleted;
  • explicit acceptance criteria;
  • legal, compliance, privacy conditions;
  • exact code, commands, paths, and error strings.

Do not write:

1
Run related tests.

Write:

1
Run `pnpm test -- auth`.

Saving tokens should not reduce executability.

Team Prompt Discipline

Put this in AGENTS.md or CLAUDE.md:

1
2
3
4
5
6
7
8
9
## Prompt Discipline

- Treat each task as a small work order.
- Prefer fields: goal, scope, limits, validation, output.
- Do not scan the whole repository unless explicitly requested.
- Ask before reading large logs or unrelated directories.
- Keep command output summarized; save full logs to files.
- Do not mix new feature work, refactor, docs, and cleanup in one task.
- Preserve exact paths, commands, error messages, and safety rules.

Compression Checklist

Problem Fix
Goal longer than one sentence Use verb + object
Background longer than 5 lines Compress to 3 facts
File scope unclear Use exact paths
Multiple “also” items Split into later tasks
Many constraints Split into must / forbidden / optional
Full logs pasted Keep command, failing test, key error
Full code pasted Provide paths
Repeated long-term rules Move to AGENTS.md / CLAUDE.md
Long output requirements Use list format
Unsure whether to edit First pass analysis only

Summary

AI coding prompts get too long when goals, background, constraints, logs, code, and long-term rules are mixed together.

The stable compression method is a task card: one-sentence goal, file paths as scope, explicit limits, a validation command, and fixed output. Keep only background facts that affect decisions. Keep logs short. Move long-term rules into project files. Split “also” into later tasks.

Shorter prompts do not make the Agent smarter, but they make it guess less, read fewer wrong files, and waste less context.

记录并分享
Built with Hugo
Theme Stack designed by Jimmy