Your AI Coding Tool Is Writing 2024 Code: Why Context Engineering Is the Only Fix That Works

1 comment
(AI Coding Tools) - AI coding tools hallucinate your codebase's conventions. Here's why context engineering—not better prompts—is the only way to stop them from writing garbage that breaks your style guide.

Your AI Coding Tool Is Writing 2024 Code: Why Context Engineering Is the Only Fix That Works

You’ve been there. You ask your AI coding tool to refactor a legacy module. It spits out 200 lines of clean, idiomatic code. Looks great. You merge it.

Two days later, your senior dev flags it: “This doesn’t follow our error-handling pattern. It’s using `console.error` instead of our custom logger. And where’s the tracing context?”

I Used the GitHub API to Profile 500 Active Repos: The 5 Metrics That Predict Open Source Longevity

I Used the GitHub API to Profile 500 Active Repos: The 5 Metrics That Predict Open Source Longevity

I Used the GitHub API to Profile 500 Active Repos: The 5 Metrics That Predict Open Source Longevity… ...

The AI wrote *correct* code. It just wrote code for a different codebase.

This isn’t a bug. It’s a feature of how these tools work. And it’s the single biggest productivity killer I see teams struggle with.

I Pitched 4 AI Coding Agents Against a Nasty Race Condition — Only One Came Back Clean

I Pitched 4 AI Coding Agents Against a Nasty Race Condition — Only One Came Back Clean

I Pitched 4 AI Coding Agents Against a Nasty Race Condition — Only One Came Back Clean Let’s… ...

The Core Problem: Your AI Has No Memory of Your Codebase

Here’s the uncomfortable truth: most AI coding tools operate on a per-file or per-session context window. They don’t know your project’s conventions, your team’s style guide, or your custom abstractions.

I’ve seen this firsthand. Recently, we onboarded a new developer in our Can Tho hub to help a US client modernize a Python monolith. The client had a strict convention: all database queries must go through a custom `Repository` base class, never raw SQLAlchemy sessions.

The AI tool? It generated raw session queries in every single file. We spent more time fixing the AI’s output than we saved using it.

The fix isn’t better prompts. It’s context engineering.

What Is Context Engineering?

Context engineering is the practice of explicitly feeding your AI coding tool the *rules of your codebase* before asking it to generate code. It’s not about writing better prompts. It’s about building a reusable, version-controlled context that your AI consumes automatically.

Think of it as a `.context` file for your project. A living document that tells the AI: “Here’s how we do things around here.”

The Three Layers of Context Engineering

Layer 1: Project-Level Conventions

  • Coding style (PEP 8, Google style, custom)
  • Error handling patterns
  • Logging and tracing requirements
  • Testing frameworks and coverage expectations

Layer 2: Architecture-Specific Rules

  • Database access patterns (repository pattern, active record, etc.)
  • API design conventions (RESTful, GraphQL, gRPC)
  • Dependency injection patterns
  • Configuration management

Layer 3: Team Workflows

  • Code review expectations
  • Commit message format
  • Branch naming conventions
  • CI/CD pipeline requirements

How to Build Your Context Engineering Pipeline

Let’s get practical. Here’s the exact setup we use with our teams in Ho Chi Minh City and Can Tho.

Step 1: Create a `CONTEXT.md` File

Don’t put this in your README. Create a dedicated file at the root of your project.

markdown
# Project Context for AI Coding Tools

## Language & Framework
- Python 3.11+
- FastAPI for REST APIs
- SQLAlchemy 2.0 with async sessions
- Pydantic v2 for data validation

## Database Access
- ALL database queries must use the `Repository` base class
- Never use raw SQLAlchemy sessions outside of `Repository` methods
- Always use async queries with `async with session.begin():`
- Example:

class UserRepository(Repository[User]):

async def find_active_users(self) -> list[User]:

async with self.session.begin():

result = await self.session.execute(

select(User).where(User.is_active == True)

)

return result.scalars().all()



## Error Handling
- Use custom `AppError` exceptions, never generic `Exception`
- All API endpoints must return structured error responses
- Log errors using `logger.error()` with trace_id context
- Never use `print()` or `console.log()`

## Testing
- pytest with async fixtures
- Minimum 80% coverage for new code
- Mock external services, never hit production APIs
- Use `pytest-asyncio` for async tests

Step 2: Automate Context Injection

Don’t manually copy-paste this file. Use your AI tool’s API or a pre-processing script.

For Cursor, you can add it to `.cursorrules`. For Copilot, use a custom instruction file. For Claude Code, use the `–context` flag.

Here’s a simple bash script we use:

bash
#!/bin/bash
# inject-context.sh
# Prepends CONTEXT.md to your prompt for AI coding tools

CONTEXT_FILE="CONTEXT.md"
PROMPT_FILE="$1"

if [ ! -f "$CONTEXT_FILE" ]; then
    echo "Error: CONTEXT.md not found in project root"
    exit 1
fi

if [ ! -f "$PROMPT_FILE" ]; then
    echo "Error: Prompt file not provided"
    exit 1
fi

cat "$CONTEXT_FILE" "$PROMPT_FILE" > /tmp/combined-prompt.md
echo "Context injected. Output: /tmp/combined-prompt.md"

Step 3: Version Control Your Context

This is critical. Your `CONTEXT.md` should live in your repository and evolve with your codebase. When you update your error handling pattern, update the context file. When you adopt a new testing framework, update the context file.

Treat it like documentation. Because it is.

The Results: What We Measured

We ran a controlled experiment with our team in Vietnam. Two groups of developers working on the same legacy codebase refactoring task.

Group A: Used AI coding tools with default settings.

Group B: Used AI coding tools with our context engineering pipeline.

Metric Group A Group B
Time to complete refactoring 4.2 hours 2.8 hours
Code review rework rate 34% 11%
Convention violations per 100 LOC 8.3 1.2
Developer satisfaction (1-10) 5.2 8.7

The numbers speak for themselves. Context engineering cut rework by nearly 70%.

Common Mistakes Teams Make

Mistake 1: Writing context once and forgetting it. Your codebase evolves. Your context must too. Set a quarterly review cycle.

Mistake 2: Making context too verbose. Keep it under 200 lines. Focus on what’s unique to your project, not generic best practices.

Mistake 3: Not testing context effectiveness. Run a simple test: ask your AI tool to generate a function without context, then with context. Compare the outputs. If they’re identical, your context isn’t specific enough.

The Future: Automated Context Generation

We’re working on something at ECOA AI that auto-generates context files by scanning your codebase. It analyzes commit history, code review comments, and common patterns to build a context file automatically.

But you don’t need to wait for that. Start with a manual `CONTEXT.md` today. It takes an hour to write and saves days of rework.

Honestly, the biggest lie in AI coding tools right now is that they “understand your codebase.” They don’t. They understand patterns from millions of public repositories. Your codebase is unique. Treat it that way.

Frequently Asked Questions

Q: How often should I update my CONTEXT.md file?

A: Update it whenever you change a core convention—error handling patterns, database access layers, testing frameworks. At minimum, review it quarterly. We tie ours to our sprint retrospectives.

Q: Does context engineering work with all AI coding tools?

A: Yes, but the implementation varies. Cursor supports `.cursorrules`, Copilot has custom instructions, Claude Code accepts `–context` flags. The principle is the same: feed the tool your project’s rules before asking it to generate code.

Q: Can I have multiple context files for different parts of my project?

A: Absolutely. We use a root `CONTEXT.md` for global conventions and module-specific context files for microservices with unique patterns. Just make sure the root file references the module files.

Q: Will context engineering slow down my AI tool’s response time?

A: Negligibly. The context file adds maybe 100-200 tokens to your prompt. The trade-off is massive: you get code that actually follows your conventions on the first try, not the third.

Related reading: Outsourcing Software Development: The Offshore Engineering Playbook for 2024

Related reading: Why You Should Hire Vietnamese Developers in 2025: The Strategic Edge for Tech Leaders

Leave a Comment

Your email address will not be published. Required fields are marked *

Ready to Build with AI-Powered Developers?

Hire Vietnamese engineers augmented by ECOA AI Platform + Claude Code. 5x faster, 40% cheaper.