Skip to content
content_strategy

Context Engineering: How to Prevent AI from Generating Low-Quality Code Using the RPI Framework

Do you know why AI works great for small projects but fails in legacy codebases? Learn about Context Engineering and the RPI workflow to prevent AI from generating low-quality code (Slop).

H
Hamze Zare Nasiri
Context Engineering: How to Prevent AI from Generating Low-Quality Code Using the RPI Framework

Introduction: The Hidden Problem with AI-Assisted Coding

If you've used AI coding tools like GitHub Copilot, Cursor, or Claude Code, you're probably familiar with this paradox: when starting a new, small project (Greenfield), everything works amazingly. Clean, fast, functional code is generated. But when you enter a legacy, complex codebase (Brownfield), the story changes.

Many developers report that although code delivery speed has increased, a significant portion of their time is spent fixing bad code that AI wrote last week. This low-quality code, known in the developer community as "Slop", can create significant Technical Debt.

In this article, you'll learn about a scientific and practical approach called Context Engineering and the RPI Framework that helps you solve complex problems in legacy code without producing Slop.


What is Context Engineering?

To understand Context Engineering, we first need to understand how Large Language Models (LLMs) work.

The Stateless Nature of Language Models

Language models are stateless. This means they have no memory of previous conversations, and the only thing that determines output is the input tokens. Everything you put in the Context Window directly affects response quality.

The "Dumb Zone" Concept

Research shows that when more than 40 percent of the Context Window is filled, model performance significantly decreases. Dex Horthy from HumanLayer calls this phenomenon the "Dumb Zone."

The Cluttered Desk Analogy: Think of the Dumb Zone like a cluttered desk. When your desk is full of scrap paper, old notes, and unnecessary tools, finding what you actually need becomes difficult and your work quality decreases. The AI's Context Window works exactly the same way.

Solution: Smart Compaction

The solution is Smart Compaction. Instead of blindly accumulating information in context, you should:

  • Intelligently summarize conversation history
  • Only include relevant files in context
  • Remove long JSON outputs, logs, and test results
  • Use Sub-agents to summarize information

The RPI Framework: A Practical Solution for Slop-Free Coding

To solve complex problems in legacy codebases, you shouldn't immediately start coding. The RPI Framework is a three-stage workflow that prevents Slop production:

1. Research - Compressing Truth

In this stage, the goal is complete system understanding and finding the correct files. You're not writing code yet; you're "Compressing Truth."

Key Techniques:

  • Use sub-agents to read large code sections and return only useful summaries
  • Identify project structure, architectural patterns, and dependencies
  • Understand Entry Points and data flow

2. Plan - Compression of Intent

After understanding the system, write a detailed execution plan. This plan should include:

  • Exact file names that need to change
  • Actual code snippets to be added or modified
  • Order of changes
  • Tests that need to be written

This stage is called "Compression of Intent." A good, detailed plan ensures even weaker models make fewer mistakes because ambiguity is minimized.

3. Implement - Final Code Production

Only after completing the previous two stages should you enter the implementation phase. In this stage:

  • Execute the plan step by step
  • Verify each change with relevant tests
  • Keep context clean and remove unnecessary information

The Human Role: Don't Outsource Thinking

One of developers' biggest mistakes is thinking AI can replace thinking. This assumption is dangerous.

AI is an Amplifier, Not a Replacement

AI is an amplifier of your abilities, not a replacement. The most dangerous thing is allowing AI to produce code without reviewing the Plan. This is equivalent to:

  • Letting an inexperienced junior make architectural decisions
  • Blindly accepting any suggestion without understanding consequences

Mental Alignment

Human review of the plan creates "Mental Alignment" in the team. When someone reviews the AI's plan:

  • Logical errors are identified before coding
  • Team knowledge increases
  • Architectural decisions are documented

What to Focus On

Instead of reading generated code line by line, focus on reviewing "Research" and "Plan". If these two stages are correct, the final code will very likely be correct too.


Context Window Management: Stay in the Smart Zone

For effective AI-assisted coding, you need to learn to stay in the "Smart Zone." This means:

Golden Rules of Context Management

  1. Under 40%: Always fill less than 40% of the Context Window
  2. Continuous Compaction: Remove unnecessary information after each stage
  3. Sub-agents: Use sub-agents for reading large files
  4. Summarization: Periodically summarize conversation history

Conclusion: The Future Belongs to Adaptive Teams

Using AI in coding is no longer a choice but a competitive necessity. However, the difference between teams that succeed and teams drowning in technical debt lies in how they use it.

The future belongs to teams that:

  • Learn to manage context and stay in the Smart Zone
  • Use the RPI Framework (Research, Plan, Implement)
  • Don't outsource thinking and review plans
  • Adapt their processes to tools that write 99% of code

By implementing Context Engineering principles and the RPI Framework, you can benefit from AI's amazing advantages without falling into the trap of producing low-quality code.


This article is based on practical experiences and research by Dex Horthy from HumanLayer on optimizing AI use in software development.

Share This Article