Agent prompt engineering is the practice of writing prompts that turn LLMs into reliable autonomous workers. Unlike chat prompting, agent prompts must produce structured tool calls, recover from errors, and stay on task across many turns.
Most agent failures are prompt failures. This page covers what works.
An agent prompt typically defines:
The system prompt is loaded once; the conversation evolves with tool calls and results.
Effective agent system prompts have a consistent structure:
[Role]
You are an agent that ___.
[Tools]
Available tools: ___ (with descriptions)
[Process]
For each task:
1. ___
2. ___
3. ___
[Output format]
Tool calls in format: ___
Final answer in format: ___
[Constraints]
Never: ___
Always: ___
[Error handling]
If a tool fails: ___
If you're stuck: ___
Each section serves a specific purpose. Skipping any creates failure modes.
Tools are the agent's hands. Bad descriptions = bad usage.
search_wiki(query): Searches the wiki.
search_wiki(query: str)
Searches the wiki for pages matching the query.
Use for: finding existing pages on a topic.
Don't use for: getting full content (use get_page).
Returns: list of {title, snippet, url}.
Example: search_wiki("authentication") →
[{"title": "AuthOverview", "snippet": "...", "url": "..."}]()
The verbose version saves tokens overall by reducing failed tool calls.
Many agents benefit from "think before acting" patterns:
Modern LLMs do this naturally with a hint. Explicit reasoning helps for complex tasks.
Most reliable for tool calls. Use schemas.
Risk: malformed JSON. Mitigate with constrained generation (Outlines, Instructor) or repair logic.
Native function calling APIs (OpenAI, Anthropic) handle JSON formatting reliably.
For human-readable outputs. Less reliable for parsing.
Anthropic's models work well with XML-tagged outputs:
<thinking>...</thinking>
<answer>...</answer>
Pick a format and be consistent.
Agents must handle failures:
Retry with backoff: transient errors.
Reformulate: if a tool fails, try different parameters.
Fallback tool: if primary fails, use alternative.
Ask for help: explicit "I need clarification" path.
Fail loudly: don't silently swallow errors.
The system prompt should describe these patterns explicitly.
Agents need clear stop signals:
Common patterns:
Many agent frameworks include detection for these.
Tell the agent what NOT to do:
Constraints are tested at every decision point. Be specific.
For complex tasks, include examples:
Example task: Fix the broken authentication
Example trace:
read_file('auth.py') → ...
detect bug
edit_file('auth.py', fix)
run_tests() → pass
Done.
Few-shot examples in agent prompts dramatically improve quality.
Long agent runs blow context windows.
Each adds complexity. Start simple.
When tasks need multiple specialized agents:
Manager agent delegates to worker agents.
Sequential specialists (research → write → review).
Multiple agents propose; vote.
Adds reliability but multiplies cost. Use only when needed.
Long system prompts cost on every turn.
Trim:
But: don't trim what's actually needed. Quality regressions from prompt cuts are common.
Without tests, prompts drift.
Build evals before iterating.
"You are a helpful assistant" → undefined behavior.
Agent does what wasn't intended because nothing forbade it.
Agent uses tools wrong; wastes turns.
Agent gives up or hallucinates after first failure.
Mix of JSON and markdown. Parsing breaks.
Prompt updated without testing. Quality regression unnoticed.
Hundreds of rules; agent loses the goal.
Agent loops forever or until token budget exhausted.
You complete tasks by calling tools.
For each request:
1. Identify which tool(s) you need
2. Call them with appropriate arguments
3. Observe results
4. Iterate or return final answer
Tools:
- search(query): full-text search, returns top 10 results
- read(url): get content of URL
- write(url, content): create or update document
Format tool calls as JSON.
Format final answer as plain text after all tool calls.
Never call write without first reading.
Stop after 10 turns or when goal complete.
You write and modify code to complete tasks.
Process:
1. Read relevant files
2. Plan changes
3. Make edits
4. Run tests
5. Iterate until tests pass
Tools: read_file, edit_file, run_command
Never:
- Modify files outside the project
- Skip tests
- Force-push
If tests fail after 3 fix attempts, ask for help.
Agent prompts evolve. Track:
Treat prompts as code: versioned, tested, reviewed.