• unwind ai
  • Posts
  • The Context Trap Every Agent Builder Falls Into

The Context Trap Every Agent Builder Falls Into

Why Adding More Rules Makes Your Agent Worse

You've been here.

You spin up an agent with minimal instructions. It hallucinates a library that doesn't exist, ignores your directory structure, and confidently writes code that won't compile. So you add more rules.

Now it won't do anything. It refuses creative solutions because three of your rules technically conflict. It asks for clarification on every step. You've built a bureaucrat, not an assistant.

This is the classical dilemma of context engineering: too little context produces chaos, too much produces paralysis.

Why This Happens

The context window isn't just instructions. It's everything the model sees before generating a response: system prompt, conversation history, retrieved documents, tool definitions, previous outputs.

Every piece competes for attention.

Add too few constraints and the model fills gaps with its training distribution—which may not match your codebase, your conventions, or reality.

Add too many and you create:

  • Attention dilution: Important rules get lost in noise

  • Conflict zones: Rule A says "always use TypeScript," Rule B says "match the existing file's language," the file is JavaScript

  • Rigidity: The model treats every rule as inviolable, losing the judgment to break one when context demands it

A 2025 study (LLMs Get Lost In Multi-Turn Conversation) tested 15 models including o3, GPT-4.1, Gemini 2.5 Pro, and Claude 3.7 Sonnet on multi-turn tasks. An average drop of 39% across six generation tasks when information was gathered across turns instead of given upfront. Even reasoning models like o3 and Deepseek-R1 deteriorate in similar ways to non-reasoning models.

The researchers named this "lost in conversation": when LLMs take a wrong turn in multi-turn conversation, they get lost and do not recover.

The Fix: A Hierarchy, Not a Pile

Stop treating rules as a flat list. Build a pyramid.

Layer 1: Constitution (3-5 rules, max)

These are non-negotiables. They apply everywhere, always.

- Security first: never log secrets or PII
- All code must pass type checking
- Write tests for new functionality

Keep this layer small. If everything is critical, nothing is.

Layer 2: Workspace Rules
Context specific to this repo, this project, this team.

- This repo uses pytest, not unittest
- Components go in src/components/
- We use Tailwind, no custom CSS

These override nothing in Layer 1, but they're always loaded for this workspace.

Layer 3: Workflows (On-Demand)

These aren't standing rules. They're scripts you trigger when needed.

Don't make "write unit tests" a permanent instruction that fires on every interaction. The agent will try to write tests while you're still scaffolding, or refuse to proceed until tests exist. Instead, make it a workflow you invoke when a feature is complete.

The same applies to "run linting," "update documentation," or "check for security issues." These are actions, not constraints. Treat them as callable workflows, not ambient rules.

The Model Upgrade Trap

Structure alone doesn't solve the problem. You've built a pyramid, but what happens when Layer 1 says one thing and Layer 2 says another?

This is where most agent setups quietly break.

Your global rule says "always use TypeScript." Your workspace rule says "match the existing file's language." The file you're editing is JavaScript. The agent freezes, or worse - outputs some hybrid that satisfies neither.

You need a priority clause.

Explicitly tell the agent which instructions win when they conflict. Add this to your system prompt:

When instructions conflict, follow this hierarchy:
1. Current user request (highest priority)
2. Workspace rules
3. Global rules (lowest priority)

This does two things.

First, it resolves ambiguity. The agent no longer has to guess whether your global style guide outweighs the repo-specific exception. You've told it: workspace wins.

Second, it gives the agent permission to break rules. Without this clause, agents treat every instruction as inviolable. You ask it to write a quick Python script, but your global rules say "always use TypeScript." The agent either refuses ("I cannot do that because your rules specify TypeScript") or contorts itself into some unholy TypeScript-that-calls-Python solution.

With the priority clause, the agent understands: if you explicitly asked for Python, your request outranks the global default. It can comply without having an existential crisis.

A practical example:

Say your setup looks like this:

  • Global rule: "Use conventional commits for all commit messages"

  • Workspace rule: "This repo uses a custom commit format: [JIRA-XXX] description"

  • User request: "Commit this with message 'quick fix'"

Without a priority clause, the agent might:

  • Refuse because none of the formats match

  • Try to combine all three into something unreadable

  • Pick one arbitrarily and hope

With a priority clause, it knows: user request wins. It commits with "quick fix." If you wanted the repo format, you'd have said so.

Rules vs. Prompts

A common mistake: putting business logic in rules.

Bad rule: "Always implement bubble sort when asked for sorting."

This is too specific. It creates a conflict the moment you need QuickSort for a performance-critical path. Now you're either overriding your own rule or watching the agent implement an O(n²) sort on a million records.

Good rule: "Include time and space complexity in docstrings for all algorithms."

This is a style constraint. It applies universally regardless of which algorithm you choose. It shapes how code gets written without dictating what gets written.

The distinction:

Rules should be durable—they survive across hundreds of interactions without needing exceptions. If you constantly override a rule, it was never a rule. It was a preference you incorrectly promoted to policy.

Before adding a rule, ask: "Is there a legitimate scenario where I'd want the opposite?"

  • "Always use TypeScript" → Yes. Quick scripts, legacy codebases, specific library constraints. This is a default, not a rule.

  • "Never commit secrets to the repo" → No. There's no legitimate exception. This is a rule.

Think of rules as guardrails on a highway. They keep you from driving off a cliff, but they don't tell you where to go. That's what the steering wheel (your prompt) is for.

Rules constrain form. Prompts specify intent. When business logic leaks into rules, you get an agent that either can't adapt or fights you when you have a good reason to deviate.

Keep them separate.

The Negative Constraint Trap

LLMs struggle with excessive negation.

"Don't write spaghetti code. Don't use deprecated APIs. Don't create files outside the project root. Don't..."

Each negation consumes attention without giving direction. It's a minefield, not a map.

Reframe positively:

Instead of

Write

Don't write spaghetti code

Separate distinct logic into dedicated modules

Don't use deprecated APIs

Check API documentation dates before using

Don't create files outside project root

All new files must be within ./src/

Positive constraints guide. Negative constraints only forbid.

Treat Context as Code

Your .cursorrules, CLAUDE.md, .windsurfrules—these are source code for your agent. Maintain them like code.

Monthly audit: Read your rules. Do they still apply?

Deprecation: If a rule hasn't been relevant in 10 sessions, delete it. Zombie rules clog the context window and increase hallucination rates.

Version control: Track changes. When the agent starts misbehaving, you can diff what changed.

Context engineering isn't about scripting every move. It's about building guardrails that let the agent run fast without crashing.

Too few guardrails: it runs off the road. Too many: it can't move.

The sweet spot is a hierarchy with clear priorities, positive constraints, and regular pruning.

You're not writing a rulebook. You're designing an environment.

We share in-depth blogs and tutorials like this 2-3 times a week, to help you stay ahead in the world of AI. If you're serious about leveling up your AI skills and staying ahead of the curve, subscribe now and be the first to access our latest tutorials.

Don’t forget to share this tutorial on your social channels and tag Unwind AI (X, LinkedIn, Threads) to support us!

Reply

or to participate.