Skip to content
SEWWA

Blog

Context Engineering for AI Coding Agents: AGENTS.md Guide

Your AI coding agent doesn’t understand your codebase. Not by default, anyway.

Every time you start a session, it has to discover your project’s structure, infer conventions, and piece together architecture from fragments. That costs tokens, slows down execution, and leads to avoidable mistakes.

Context engineering is the practice of giving agents the right information upfront, so they make better decisions faster.

Here’s how AGENTS.md and agent skills work, and how to use them effectively.


The Problem: Repeated Discovery Cost

Without proper context, every coding session starts from zero.

What happens without context:

  1. Agent reads system prompt
  2. Agent explores repository structure
  3. Agent infers conventions from code
  4. Agent makes assumptions about architecture
  5. Agent generates code
  6. You correct mistakes
  7. Agent learns from corrections
  8. Session ends, understanding disappears

Cost: High token usage, slow execution, repeated mistakes.


What Is AGENTS.md?

AGENTS.md is a Markdown file at the root of your repository that gives AI agents spatial awareness of your codebase.

Think of it as the agent-facing counterpart to README.md. While README.md explains your project to humans, AGENTS.md explains it to AI agents.

What Goes in AGENTS.md?

A strong AGENTS.md includes:

  1. Project overview: What the product does and who it’s for
  2. Tech stack: Frameworks, languages, databases, tools
  3. Architecture decisions: Key patterns and why they exist
  4. Coding conventions: Naming, structure, formatting rules
  5. Operational boundaries: What not to modify or call

Example AGENTS.md

# Project Overview
E-commerce platform built with Next.js 14 and TypeScript.
Serves 100k+ daily active users.
# Tech Stack
- Next.js 14 (App Router)
- TypeScript 5.3
- PostgreSQL + Prisma ORM
- Tailwind CSS
- Vercel deployment
# Architecture
- /app routes use Server Components by default
- Client Components marked with 'use client'
- API routes in /app/api/
- Database queries through Prisma client only
# Coding Conventions
- Use TypeScript strict mode
- Prefer Server Components for data fetching
- Client Components only for interactivity
- All database mutations through Prisma
- Never use useEffect for data fetching
# Operational Boundaries
- Do NOT modify /prisma/schema.prisma without approval
- Do NOT call external APIs without error handling
- Do NOT bypass authentication checks
- Always use transactions for multi-step mutations

What Are Agent Skills?

If AGENTS.md gives agents a map of your codebase, agent skills give them reusable expertise for specific tasks.

How Skills Work

A skill is typically a folder containing:

Skill Examples

Authentication Skill:

/skills/authentication/
- SKILL.md
- examples/
- clerk-setup.ts
- nextauth-config.ts
- templates/
- protected-route.tsx
- middleware.ts

Database Migration Skill:

/skills/database-migration/
- SKILL.md
- scripts/
- generate-migration.sh
- test-migration.sh
- templates/
- migration-template.ts

Progressive Disclosure

Skills load when relevant, not upfront. Instead of front-loading every rule, agents pull in the skill that matches the task.

This keeps context lean while ensuring domain-specific guidance is available when needed.


How AGENTS.md Works in Practice

Step 1: Agent Reads AGENTS.md

When a session starts, the agent checks for AGENTS.md at the repository root.

Step 2: Context Shapes Understanding

The agent uses AGENTS.md to:

Step 3: Better Code Generation

With context, the agent produces code that:


Best Practices for AGENTS.md

1. Keep It Lean

Don’t: Document every function and component Do: Focus on high-level patterns and constraints

A bloated AGENTS.md wastes context space and weakens signal-to-noise ratio. Aim for orientation, not exhaustive documentation.

2. Be Specific About Boundaries

Vague: “Be careful with database changes” ✅ Specific: “Do NOT modify /prisma/schema.prisma without team approval. All schema changes go through migration review process.”

Specific constraints prevent more mistakes.

3. Include Commands

List the commands agents are most likely to need:

# Common Commands
- `npm run dev` - Start development server
- `npm run build` - Build for production
- `npm run test` - Run test suite
- `npx prisma studio` - Open database GUI
- `npx prisma migrate dev` - Create and apply migration

This reduces discovery time for routine tasks.

4. Document Tech Stack Explicitly

Don’t assume the agent knows your stack:

# Tech Stack
- Framework: Next.js 14 (App Router)
- Language: TypeScript 5.3 (strict mode)
- Database: PostgreSQL 15
- ORM: Prisma 5.7
- Styling: Tailwind CSS 3.4
- Deployment: Vercel

Explicit stack documentation prevents architectural mismatches.

5. Explain Why, Not Just What

What: “Use Server Components by default” ✅ Why: “Use Server Components by default to minimize client bundle size and improve initial page load. Our Lighthouse target is 95+ for all pages.”

Context helps agents make better decisions when rules don’t cover edge cases.


How Agent Skills Work in Practice

Example: Implementing Authentication

Without skill:

  1. Agent researches authentication patterns
  2. Agent explores existing auth code
  3. Agent makes assumptions about your setup
  4. Agent generates code
  5. You correct mistakes

With authentication skill:

  1. Agent loads authentication skill
  2. Agent reads your auth patterns from SKILL.md
  3. Agent uses your templates and examples
  4. Agent generates code following your conventions
  5. Fewer corrections needed

Skill Structure Best Practices

1. Clear Purpose:

SKILL.md
## Purpose
Implement authentication flows using Clerk in Next.js applications.
## When to Use
- Adding new protected routes
- Implementing sign-in/sign-up flows
- Setting up role-based access control

2. Step-by-Step Workflow:

## Workflow
1. Install dependencies: `npm install @clerk/nextjs`
2. Add environment variables (see .env.example)
3. Wrap app in <ClerkProvider>
4. Create middleware.ts for route protection
5. Add <SignIn /> and <SignUp /> components

3. Code Templates:

templates/protected-route.tsx
import { auth } from '@clerk/nextjs'
import { redirect } from 'next/navigation'
export default async function ProtectedRoute() {
const { userId } = auth()
if (!userId) {
redirect('/sign-in')
}
// Your protected content here
}

4. Common Mistakes to Avoid:

## Common Mistakes
❌ Using `useAuth()` in Server Components
✅ Use `auth()` from '@clerk/nextjs' in Server Components
❌ Forgetting to add route to middleware matcher
✅ Update middleware.ts config when adding new protected routes

Real-World Comparison

I tested the same task three ways:

Task: Implement role-based access control (RBAC) for a Next.js app

Test 1: No Context

Test 2: AGENTS.md Only

Test 3: AGENTS.md + RBAC Skill

Result: Context engineering reduced token usage by 54% and eliminated mistakes.


When to Use AGENTS.md vs Skills

Use AGENTS.md for:

Use Skills for:

Use Both when:


Implementation Checklist

Phase 1: Create AGENTS.md

Phase 2: Identify Skill Opportunities

Phase 3: Build Your First Skill

Phase 4: Iterate


Common Mistakes to Avoid

1. Over-Documenting

Too much: 5,000-word AGENTS.md covering every edge case ✅ Right size: 500-1,000 words focusing on high-level patterns

2. Vague Constraints

Vague: “Follow best practices” ✅ Specific: “Use Server Components by default. Client Components only for interactivity requiring useState or event handlers.”

3. Outdated Information

Stale: AGENTS.md from 3 months ago, tech stack changed ✅ Current: Update AGENTS.md when architecture changes

4. Skill Bloat

Too many skills: 20+ skills loaded for simple project ✅ Focused skills: 3-5 skills for most common workflows

5. Missing Context

Incomplete: AGENTS.md without tech stack or boundaries ✅ Complete: All sections filled with specific, actionable information


Measuring Success

Metrics to Track

  1. Token usage: Should decrease 30-50%
  2. Mistake rate: Should drop to near-zero
  3. Correction time: Should reduce significantly
  4. Code consistency: Should improve measurably

How to Track


The Future: Context-Aware Development

Context engineering is becoming essential as AI coding tools mature.

Phase 1 (Now): AI helps write code, but needs hand-holding Phase 2 (Coming): AI understands your project through context Phase 3 (Future): AI proactively suggests improvements based on context

Teams investing in context engineering now will have advantages as AI tools evolve.


Key Takeaways

  1. Context engineering reduces discovery cost: Give agents information upfront instead of making them explore

  2. AGENTS.md provides spatial awareness: A map of your codebase for AI agents

  3. Skills provide domain expertise: Reusable patterns for specific workflows

  4. Specificity prevents mistakes: Detailed constraints > vague guidelines

  5. Measure and iterate: Track token usage, mistakes, and consistency

  6. Start small: AGENTS.md first, then build skills for common workflows


What to Do Today

  1. Create AGENTS.md at your repository root
  2. Document your tech stack explicitly
  3. Define operational boundaries clearly
  4. Identify your most repetitive workflow for your first skill
  5. Test with a real task and measure improvement

Context engineering isn’t about teaching AI everything—it’s about giving it the right starting point so it makes better decisions faster.

Your future self (and your token budget) will thank you.


Need structured data for your AI agents? Check out SEWWA’s Schema Generator to create JSON-LD markup that helps both search engines and AI agents understand your content.