Prompting for AI Agents

Writing system prompts, tool descriptions, and role definitions for agents. 80% of agent success is prompt quality, not the model.

🇷🇺 Russian version: prompting.ru.md


← AI agents · Memory · Safety →


Contents

  1. How agent prompting differs
  2. System prompt structure
  3. Tool descriptions
  4. Prompts for different roles
  5. Few-shot for agents
  6. Anti-patterns
  7. Whats next

1. How agent prompting differs

Regular prompt Agent prompt
“Answer briefly” “If unsure, call a tool — dont guess”
“Be polite” “Explain your plan before each action”
“Use facts” “Always verify facts through tools”
“Format: JSON” “Return structured output by schema”

An agent prompt must describe behavior rules, not just output format. The model needs to know:


2. System prompt structure

SYSTEM_PROMPT = """
## ROLE
You are a Senior Python Developer in the project team.

## TOOLS
You have access to:
- read_file(path) — read a file
- write_file(path, content) — write a file
- run_tests(path) — run tests
- search_code(query) — search codebase

## RULES
- Always read existing code before making changes
- Write tests first (TDD)
- If tests fail, fix them — dont ask
- Do not delete code unnecessarily

## BOUNDARIES
- Do NOT modify files outside src/
- Do NOT touch config files (.env, config/*)
- Do NOT use sudo or system commands

## OUTPUT FORMAT
- First explain what you will do
- Then show the code
- Then run tests
"""

You can build prompts programmatically:

def build_agent_prompt(role: str, tools: list[str], rules: list[str]) -> str:
    """Assembles an agent system prompt from components."""
    tools_str = "\n".join(f"- {t}" for t in tools)
    rules_str = "\n".join(f"- {r}" for r in rules)

    return f"""
## ROLE
{role}

## TOOLS
{tools_str}

## RULES
{rules_str}

## OUTPUT FORMAT
First explain what you'll do, then execute, then show results.
"""

3. Tool descriptions

Tool descriptions are parsed by the model to decide when to call them. Poor descriptions cause wrong tool selection.

Bad

{
    "name": "search",
    "description": "Search function",
    "parameters": {...}
}
# → The model wont know when to call this

Good — specify WHEN and WHEN NOT

{
    "name": "search_web",
    "description": (
        "Search the web via Google. "
        "Use THIS tool when you need current data not in your training set. "
        "EXAMPLE: news, prices, dates, weather, exchange rates. "
        "Do NOT use for general questions (what is Python, capital of France)."
    ),
    "parameters": {
        "type": "object",
        "properties": {
            "query": {
                "type": "string",
                "description": "Search query. Be specific about what you need."
            }
        },
        "required": ["query"]
    }
}

Best practices

  1. Specify WHEN to use — “Use for searching current data”
  2. Specify WHEN NOT to use — “Do NOT use for general questions”
  3. Give examples — “EXAMPLE: news, prices, weather”
  4. Descriptive names — search_web not func_1
  5. Describe parameters — what to put in query, what format

4. Prompts for different roles

PM Agent

PM_PROMPT = """
You are a Project Manager. Your team: analyst, developer, tester, DevOps.

TASKS:
1. Break the task into subtasks
2. Assign each subtask to the right agent
3. Track deadlines
4. Review results

FORMAT:
[Task]: description
[Agent]: name
[Deadline]: estimate
[Done]: criteria
"""

Developer Agent

DEV_PROMPT = """
You are an experienced developer. Write clean, tested code.

PRINCIPLES:
1. Read existing code first
2. Understand the architecture
3. Write tests (TDD)
4. Implement the feature
5. Verify tests pass

STYLE:
- All functions must have type hints
- All public functions need docstrings
- Single responsibility principle
- Keep functions under 50 lines
- No TODO, FIXME, or print statements

TOOLS: read_file, write_file, run_tests, search_code
"""

QA / Tester Agent

TESTER_PROMPT = """
You are a QA engineer. Find bugs before users do.

PROCESS:
1. Read the specification
2. Write test cases
3. Write automated tests
4. Run and check coverage
5. Report bugs with details

WHAT TO CHECK:
- Edge cases (empty, null, 0, -1)
- Errors (invalid input, missing file, no permissions)
- Load (what happens with 1000 calls?)
- Security (SQL injection, XSS, path traversal)
"""

Analyst Agent

ANALYST_PROMPT = """
You are a systems analyst. Turn vague ideas into clear tasks.

PROCESS:
1. Ask clarifying questions if requirements are unclear
2. Break into atomic tasks
3. Assess risks
4. Propose architecture

OUTPUT FORMAT:
## Requirements
- ...

## Architecture
- ...

## Risks
- ...
"""

5. Few-shot for agents

Few-shot examples help the model understand the expected behavior.

FEW_SHOT = """
Correct tool call example:

User: What is the weather in Moscow?
You should call search_web(query="weather Moscow today")
and return the result.

Incorrect:
> It's about 20 degrees in Moscow
(You didn't call the tool — this is guessing!)

Correct:
> Let me check...
> [calls search_web]
> Weather data shows +22C in Moscow
"""

Output format example

FEW_SHOT_OUTPUT = """
Good answer format:

## What was done
- Read main.py
- Found calculate_total function
- Added empty list handling

## Code
```python
def calculate_total(items):
    if not items:
        return 0
    return sum(items)

Tests


6. Anti-patterns

Too long

# Bad: 2000+ words, agent loses focus
PROMPT = "You must... (very long text)... also don't forget..."

# Good: structured, concise
PROMPT = """
## ROLE
## TASK
## TOOLS
## RULES
"""

Contradictory

# Bad: "be creative" AND "strictly follow rules"
PROMPT = "Be creative but always follow the rules..."

# Good: unambiguous
PROMPT = "Strictly follow instructions. Creativity is not required."

Vague triggers

# Bad: unclear when to call a tool
PROMPT = "You have tools. Use them."

# Good: clear triggers
PROMPT = "Call search_web ONLY when you need current (today) data."

No boundaries

# Bad: agent can do anything
PROMPT = "Make the project better."

# Good: clear boundaries
PROMPT = "Improve test coverage. Do NOT change application logic."

7. Whats next

| If you want | Go to | |————-|——-| | Agent safety (guardrails, limits) | safety.md | | Build an agent team | multi-agent.md | | Write your first agent | tutorials/01-first-agent.md | | Back | README.md | —


In section: architecture · evaluation · frameworks · memory · multi-agent · ollama-for-agents · orchestrators · patterns · prompting · ready-made · safety · skills
Related sections: Zero Level · Local Models · Use Cases · Resources
Navigation: ← AI Agents · ↑ Back to main · 🇷🇺 Русский