Structuring prompts: context, task, constraints
Use a repeatable three-part framework to give your AI agent the right context, a clear task, and explicit constraints.
12 min
Structuring prompts: context, task, constraints
Good prompts aren't magic. They follow a pattern. Once you learn it, you can apply it to any coding task — from one-liners to full features.
The three-part framework
Every effective prompt has three sections, whether you label them or not:
- Context: what already exists — files, stack, patterns, relevant code.
- Task: the single thing you want the agent to do.
- Constraints: boundaries — what to avoid, which patterns to follow, performance or style rules.
Bad prompt vs. structured prompt
A prompt without structure:
Add error handling to the API
The agent doesn't know which API, which errors, or how you handle them today.
The same request, structured:
Context: We have a Next.js Route Handler in src/app/api/projects/route.ts.
It calls getProjects() from @/lib/db and returns JSON.
Currently it has no try/catch.
Task: Add error handling to the GET handler.
Constraints:
- Catch database errors and return a 500 JSON response with { error: "Internal server error" }.
- Catch zod validation errors from query params and return 400 with the validation issues.
- Do not change the success response shape.
- Use the existing AppError class from @/lib/errors.
The structured version eliminates guessing. The agent knows exactly which file, which patterns, and what not to break.
You don't need headers every time
The framework is a mental checklist, not a rigid template. For small tasks, one sentence can cover all three parts:
In src/components/Button.tsx, add a loading prop (boolean, default false)
that disables the button and shows a spinner icon from lucide-react.
- Context:
src/components/Button.tsx - Task: add a
loadingprop - Constraints: boolean, default false, disables button, uses lucide-react
When to be extra explicit
Add more context when:
- The codebase has non-obvious conventions (custom hooks, shared types, monorepo structure).
- The task involves multiple files — list them so the agent doesn't invent new ones.
- You need a specific library or API — name the exact import, not just the concept.
The goal isn't to write an essay. It's to remove every fork in the road where the agent would guess differently than you'd choose.