Skip to content

How Do I Provide Effective Context to AI Coding Agents? A Practical Guide

I wasted three hours yesterday watching my AI coding agent generate garbage code. The solution it proposed was completely off-base, the architecture was wrong, and I had to start over from scratch.

The problem wasn’t the agent. It was me. I fed it zero context, a vague prompt, and expected magic.

The Real Problem: Context Starvation

Here’s what I did wrong:

Me: "Build me a user authentication system"
Agent: *generates generic JWT implementation*
Me: "No, that's not what I need"
Agent: *generates different generic implementation*
Me: "Still wrong..."
*3 hours later*

The agent wasn’t failing. I was failing at my job: providing context.

The Mindset Shift: Context > Prompts

I used to spend hours crafting the perfect prompt. Now I spend that time gathering context.

Here’s the key insight:

┌─────────────────────────────────────────────────────┐
│ OLD APPROACH │
│ │
│ 80% Prompt Crafting │
│ 20% Context Gathering │
│ │
│ Result: Mediocre outputs, endless iterations │
├─────────────────────────────────────────────────────┤
│ NEW APPROACH │
│ │
│ 20% Prompt Crafting │
│ 80% Context Gathering │
│ │
│ Result: Targeted, accurate solutions │
└─────────────────────────────────────────────────────┘

With agents, context is now more important than the prompt itself.

Rule #1: Think First, Then Ask

The first step to solving a problem is thinking for yourself. With AI agents, it’s too easy to ask and let the model do everything — and get bad results.

Before I open my agent, I now force myself to:

  1. Write down what I know about the problem
  2. Sketch the architecture I think I need
  3. List the constraints that matter
  4. Identify what I don’t know (gaps in my knowledge)

This takes 5-10 minutes and saves hours of back-and-forth.

┌─────────────────────────────────────────────────────┐
│ BEFORE ENGAGING AGENT │
│ │
│ ┌──────────────────┐ ┌──────────────────┐ │
│ │ What I Know │ │ What I Need │ │
│ │ - Database: PG │ │ - Auth system │ │
│ │ - Framework: │ │ - Rate limiting │ │
│ │ Flask │ │ - Token refresh│ │
│ │ - Need: JWT │ │ │ │
│ └──────────────────┘ └──────────────────┘ │
│ │
│ ┌──────────────────────────────────────────┐ │
│ │ Constraints │ │
│ │ - Must work with existing user table │ │
│ │ - Token expiry: 24 hours │ │
│ │ - Support: email + password only │ │
│ └──────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────┘

Only after this preparation do I engage the agent.

Rule #2: Treat Context Like a Budget

My context window is not infinite. Every message adds tokens, and stale context leads to hallucinations.

When agents start doing weird things mid-task, it’s usually because they’re drowning in stale context.

I’ve learned to:

┌─────────────────────────────────────────────────────┐
│ CONTEXT BUDGET RULES │
│ │
│ 1. Start fresh for complex tasks │
│ - New conversation = clean slate │
│ - Old context carries old assumptions │
│ │
│ 2. Front-load important context │
│ - Architecture decisions first │
│ - Constraints and requirements early │
│ - Code examples that matter │
│ │
│ 3. Prune aggressively │
│ - Remove solved problems from context │
│ - Summarize long discussions │
│ - Start fresh conversation when stuck │
└─────────────────────────────────────────────────────┘

Rule #3: Feed Your Agents Properly

When facing something hard, I follow a specific feeding pattern:

┌─────────────────────────────────────────────────────┐
│ CONTEXT FEEDING PATTERN │
│ │
│ Step 1: Gather raw materials │
│ ┌─────────────────────────────────────────────┐ │
│ │ • Related documentation (official) │ │
│ │ • Similar code patterns in codebase │ │
│ │ • Error messages and stack traces │ │
│ │ • Configuration files │ │
│ └─────────────────────────────────────────────┘ │
│ │
│ Step 2: Provide structure │
│ ┌─────────────────────────────────────────────┐ │
│ │ • "Here's what I'm trying to do:" │ │
│ │ • "Here's my current approach:" │ │
│ │ • "Here's where I'm stuck:" │ │
│ └─────────────────────────────────────────────┘ │
│ │
│ Step 3: Set boundaries │
│ ┌─────────────────────────────────────────────┐ │
│ │ • "Don't modify these files:" │ │
│ │ • "Must use this pattern:" │ │
│ │ • "Avoid this approach because:" │ │
│ └─────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────┘

This structure helps the agent understand not just what I want, but why I want it and what constraints matter.

Rule #4: Use Multiple Models for Hard Problems

When I’m truly stuck, I use a parallel analysis approach:

  1. Start a fresh conversation
  2. Ask the same problem to 2 different models
  3. Compare their solutions
  4. Pick the best approach (or combine them)

This sounds like more work, but it’s actually faster. Two perspectives often reveal blind spots I’d miss with just one.

┌─────────────────────────────────────────────────────┐
│ PARALLEL ANALYSIS WORKFLOW │
│ │
│ ┌─────────┐ ┌─────────┐ │
│ │ Model A │ │ Model B │ │
│ └────┬────┘ └────┬────┘ │
│ │ │ │
│ ▼ ▼ │
│ ┌───────────┐ ┌───────────┐ │
│ │ Solution A│ │ Solution B│ │
│ └─────┬─────┘ └─────┬─────┘ │
│ │ │ │
│ └──────────┬─────────────────┘ │
│ ▼ │
│ ┌───────────────┐ │
│ │ Best Solution │ │
│ │ (or hybrid) │ │
│ └───────────────┘ │
└─────────────────────────────────────────────────────┘

Real Example: Authentication System

Let me show you the difference between bad and good context.

Bad Context (what I used to do):

Build me an authentication system for my Flask app.

Good Context (what I do now):

I'm building an authentication system for a Flask app.
CONTEXT:
- Database: PostgreSQL with SQLAlchemy
- Existing user table: users (id, email, password_hash, created_at)
- Framework: Flask with Flask-Login available
- Need: JWT tokens for API access
REQUIREMENTS:
- Email + password login only
- Token expiry: 24 hours
- Refresh token: 7 days
- Rate limiting: 5 attempts per 15 minutes per IP
CONSTRAINTS:
- Must work with existing users table
- Cannot add new columns to database
- Must integrate with current Flask-Login session auth
WHAT I'VE TRIED:
- Flask-JWT-Extended but got confused with refresh tokens
- Current error: "RuntimeError: No secret key set"
Here's my current app structure:
[attached: project structure tree]
Here's the relevant code:
[attached: current auth.py and models.py]

The second approach gets me a working solution in one shot. The first approach gets me generic code I have to rewrite.

Context Preparation Checklist

Before engaging an agent for any significant task:

  • Have I thought through the problem myself?
  • Can I explain what I want in one sentence?
  • Do I know my constraints (database, framework, existing code)?
  • Have I gathered relevant documentation?
  • Can I provide code examples of what I want?
  • Have I identified what I don’t know?
  • Is this a fresh conversation (or should it be)?

The Hard Truth

Context engineering is a skill. It’s not about writing better prompts — it’s about being a better engineer first.

The agents are getting smarter. My job isn’t to outsmart them with clever prompts. My job is to feed them the context they need to do their job well.

When I do my job right, the agent does its job right. When I skip the thinking and context preparation, I get garbage.

Three hours wasted taught me that lesson.

Final Words + More Resources

My intention with this article was to help others share my knowledge and experience. If you want to contact me, you can contact by email: Email me

Here are also the most important links from this article along with some further resources that will help you in this scope:

Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!

Comments