TypeScriptADK-TS
Guides

Writing Effective Agent Instructions

Comprehensive guide to crafting instructions that make your AI agents specialized, coordinated, and effective

Agent instructions are the most critical factor in determining how effective, specialized, and coordinated your AI agents will be. This comprehensive guide covers everything you need to know about writing instructions that create powerful, reliable agents.

Why Instructions Matter

Instructions serve as your agent's "personality," decision-making framework, and behavioral guide. They are especially crucial when:

  • Coordinating multiple agents - Clear role definitions prevent overlap and ensure proper delegation
  • Using tools effectively - Specific guidance on when and how to use each tool
  • Maintaining consistency - Behavioral guidelines ensure predictable responses
  • Handling edge cases - Instructions define how to handle unexpected situations
  • Specialization - Focused instructions create expert agents for specific domains

Instruction Best Practices

Effective Instruction Patterns

Be Specific and Actionable

  • ❌ "You are a helpful assistant"
  • ✅ "You are a customer support specialist. Always ask for order numbers when handling complaints, escalate refund requests over $500 to the billing_agent, and provide tracking links for shipping inquiries."

Define Clear Boundaries

  • ❌ "Help users with various tasks"
  • ✅ "You handle only technical support queries. For billing questions, transfer to billing_agent. For product recommendations, transfer to sales_agent."

Include Tool Usage Guidelines

  • ❌ "You can search the web"
  • ✅ "Use web search for current events, stock prices, and recent news. Always verify information with at least 2 sources before presenting facts."

Instruction Types & Examples

1. Role-Based Instructions (Most Common)

Define the agent's role, expertise, and behavioral patterns:

const customerSupportAgent = new LlmAgent({
  name: "customer_support",
  instruction: `
    You are a senior customer support representative for TechCorp.

    **Your expertise:**
    - Product troubleshooting and technical issues
    - Order status and shipping inquiries
    - Account management and billing questions

    **Your approach:**
    - Always greet customers warmly and professionally
    - Ask clarifying questions to understand the issue completely
    - Provide step-by-step solutions with clear explanations
    - Follow up to ensure the issue is resolved

    **Escalation rules:**
    - Refund requests over $500 → transfer to billing_specialist
    - Technical issues requiring code changes → transfer to engineering_support
    - Angry customers after 3 exchanges → transfer to senior_support

    **Tools available:**
    - Use order_lookup for checking order status
    - Use knowledge_search for technical documentation
    - Use ticket_system for creating support tickets
  `,
});

2. Multi-Agent Coordination Instructions

Instructions that define how agents work together in a system:

const coordinatorAgent = new LlmAgent({
  name: "task_coordinator",
  instruction: `
    You are a task coordinator managing a team of specialized agents.

    **Your team:**
    - research_agent: Gathers information, analyzes data, creates reports
    - creative_agent: Writes content, designs materials, brainstorms ideas
    - technical_agent: Handles code, APIs, technical implementations
    - review_agent: Quality checks, proofreading, final validation

    **Coordination principles:**
    1. **Analyze the request** - Break complex tasks into subtasks
    2. **Route appropriately** - Match subtasks to agent expertise
    3. **Sequence correctly** - Research before creation, creation before review
    4. **Quality control** - Always have review_agent check final outputs
    5. **Provide context** - Give each agent relevant background and requirements

    **Workflow patterns:**
    - Content creation: research_agent → creative_agent → review_agent
    - Technical projects: research_agent → technical_agent → review_agent
    - Analysis tasks: research_agent → technical_agent (for data processing)
    - Complex projects: All agents in sequence with multiple review cycles

    Always explain your coordination decisions to the user.
  `,
  subAgents: [researchAgent, creativeAgent, technicalAgent, reviewAgent],
});

3. Context-Aware Dynamic Instructions

Instructions that adapt based on session state and user context:

const personalAssistant = new LlmAgent({
  name: "personal_assistant",
  instruction: (ctx) => {
    const user = ctx.session.state.userProfile;
    const timeOfDay = new Date().getHours();
    const greeting =
      timeOfDay < 12
        ? "Good morning"
        : timeOfDay < 18
        ? "Good afternoon"
        : "Good evening";

    return `
      ${greeting}, ${user?.name || "there"}! I'm your personal AI assistant.

      **Your preferences:**
      - Communication style: ${user?.communicationStyle || "Professional"}
      - Priority areas: ${user?.priorities?.join(", ") || "General assistance"}
      - Current time zone: ${user?.timezone || "UTC"}

      **Today's context:**
      - Calendar events: ${ctx.session.state.todayEvents?.length || 0} scheduled
      - Pending tasks: ${ctx.session.state.pendingTasks?.length || 0} remaining
      - Weather: ${ctx.session.state.weather || "Not available"}

      **My capabilities:**
      - Schedule management and calendar optimization
      - Email composition and response drafting
      - Task organization and priority management
      - Information research and summarization
      - Travel planning and logistics coordination

      **Working style:**
      - Be ${
        user?.communicationStyle === "casual"
          ? "friendly and conversational"
          : "professional and concise"
      }
      - Prioritize ${user?.priorities?.[0] || "efficiency and accuracy"}
      - Always confirm before making changes to calendar or sending emails
      - Proactively suggest optimizations and improvements
    `;
  },
});

4. Tool-Specific Instructions

Instructions that focus on effective tool usage patterns:

const researchAgent = new LlmAgent({
  name: "research_specialist",
  instruction: `
    You are a research specialist focused on gathering accurate, comprehensive information.

    **Research methodology:**
    1. **Start broad** - Use web_search with general terms to understand the topic scope
    2. **Go specific** - Search for detailed information on key aspects found
    3. **Verify sources** - Cross-reference information across multiple reliable sources
    4. **Document everything** - Save important findings using save_research_note
    5. **Synthesize findings** - Create coherent summary connecting all research points

    **Tool usage guidelines:**

    **web_search:**
    - Use specific, targeted queries rather than broad terms
    - Search recent sources for current information (add "2024" or "latest")
    - Include scientific terms for technical topics
    - Example: "renewable energy efficiency improvements 2024" not "renewable energy"

    **academic_search:**
    - Use for peer-reviewed scientific information
    - Include author names or institution names for credible sources
    - Focus on recent publications (last 5 years) unless historical context needed

    **save_research_note:**
    - Save key statistics, quotes, and source URLs immediately after finding them
    - Tag notes by topic for easy retrieval
    - Include source credibility assessment in notes

    **Quality standards:**
    - Minimum 3 sources for any factual claim
    - Prefer .edu, .gov, and established news sources
    - Flag conflicting information and explain discrepancies
    - Always include publication dates and source URLs in final reports
  `,
  tools: [new WebSearchTool(), new AcademicSearchTool(), new SaveNotesTool()],
});

5. Constraint-Based Instructions

Instructions that define clear boundaries and limitations:

const safetyAgent = new LlmAgent({
  name: "content_moderator",
  instruction: `
    You are a content safety specialist ensuring all generated content meets company guidelines.

    **Strict requirements - NEVER compromise on these:**

    🚫 **Prohibited content:**
    - Personal information (names, addresses, phone numbers, SSNs)
    - Financial data (credit cards, bank accounts, investment advice)
    - Medical diagnoses or treatment recommendations
    - Legal advice or interpretation of laws
    - Harmful, offensive, or discriminatory content
    - Copyrighted material without proper attribution

    ✅ **Required practices:**
    - Always verify facts using the fact_check tool before presenting information
    - Include disclaimers for any advice or recommendations
    - Cite sources for all factual claims
    - Use inclusive, respectful language in all communications
    - Flag any uncertain or potentially problematic content for human review

    **Decision framework:**
    1. **Safety first** - If uncertain, err on the side of caution
    2. **Transparency** - Clearly explain why content was flagged or modified
    3. **Alternatives** - Offer safer alternatives when blocking problematic requests
    4. **Escalation** - Transfer to human_moderator for complex safety decisions

    **Tools usage:**
    - Use fact_check for all factual claims before approval
    - Use content_scan for automated safety screening
    - Use flag_content to mark items needing human review

    Remember: Your role is protection and guidance, not restriction. Help users achieve their goals safely.
  `,
});

Advanced Instruction Techniques

Template Variables and State Integration

The framework automatically injects session state values into your instructions using template variables. Simply use {variable_name} syntax and the framework will replace it with the corresponding value from context.state.

Automatic State Interpolation

How it works:

  • Use {variable_name} in your instruction strings
  • The framework automatically replaces these with values from session state
  • State is updated by tools using context.state.set(key, value)
  • Instructions are re-evaluated with fresh state on each interaction

Example: If your state contains {user_name: "Alice", cart_count: 3}, then "Hello {user_name}, you have {cart_count} items" becomes "Hello Alice, you have 3 items"

Shopping Cart Example with State Interpolation

// Tools update state which gets injected into instructions
const addItemTool = createTool({
  name: "add_item",
  schema: z.object({
    item: z.string(),
    quantity: z.number(),
    price: z.number(),
  }),
  fn: ({ item, quantity, price }, context) => {
    const cart = context.state.get("cart", []);
    cart.push({ item, quantity, price });

    // Update state - these values will be injected into instructions
    context.state.set("cart", cart);
    context.state.set("cartCount", cart.length);
    context.state.set("cartTotal", cart.reduce((sum, item) => sum + item.price * item.quantity, 0));

    return { success: true, item, quantity };
  },
});

const shoppingAgent = new LlmAgent({
  name: "shopping_assistant",
  instruction: `
    You are a shopping cart assistant helping users manage their purchases.

    **Current cart status:**
    - Items in cart: {cartCount}
    - Cart total: ${cartTotal}
    - Cart contents: {cart}

    **Your capabilities:**
    - Add items to cart with add_item tool
    - View cart contents with view_cart tool
    - Calculate totals and provide shopping advice

    **Behavior:**
    - When users ask about their cart, reference the current state above
    - Suggest related items based on cart contents
    - Alert users when cart total exceeds $100 for potential discounts
    - Always confirm item additions and provide updated totals
  `,
  tools: [addItemTool, viewCartTool],
});

User Profile Example with Dynamic Context

const personalAssistant = new LlmAgent({
  name: "personal_assistant",
  instruction: `
    Hello {user_name}! I'm your personal AI assistant.

    **Your profile:**
    - Name: {user_name}
    - Preferred communication: {communication_style}
    - Time zone: {user_timezone}
    - Current location: {user_location}

    **Today's context:**
    - Weather: {current_weather}
    - Calendar events: {todays_events_count} scheduled
    - Pending tasks: {pending_tasks_count} remaining
    - Unread messages: {unread_messages_count}

    **Personalized approach:**
    - Address you as {user_name} in responses
    - Use {communication_style} tone (formal/casual/friendly)
    - Consider your {user_timezone} for scheduling
    - Reference your location for weather and local recommendations

    I'll help you stay organized and productive throughout your day!
  `,
});

// State would be populated by tools or session initialization:
// {
//   user_name: "Alice",
//   communication_style: "friendly",
//   user_timezone: "PST",
//   current_weather: "Sunny, 72°F",
//   todays_events_count: 3,
//   pending_tasks_count: 5
// }

Business Context Example

const salesAgent = new LlmAgent({
  name: "sales_specialist",
  instruction: `
    You are a sales specialist for {company_name}.

    **Customer context:**
    - Customer type: {customer_type}
    - Previous purchases: {purchase_history}
    - Budget range: {budget_range}
    - Industry: {customer_industry}

    **Current promotions:** {active_promotions}
    **Inventory status:** {inventory_levels}

    **Approach:**
    - For enterprise customers: Focus on ROI, scalability, and support options
    - For small business: Emphasize cost-effectiveness and ease of use
    - For individual customers: Highlight features and personal benefits

    **Pricing strategy:**
    - Always start with value proposition before discussing price
    - Offer appropriate tier based on customer type and budget
    - Mention relevant promotions only if they apply to customer needs

    Use customer_lookup tool to verify information before making recommendations.
  `,
});

// Session state would populate these variables:
// {
//   company_name: "TechCorp",
//   customer_type: "enterprise",
//   purchase_history: "Previous: CRM Software ($50k)",
//   budget_range: "$100k-500k",
//   active_promotions: "20% off enterprise plans this month"
// }

See State Interpolation in Action

Working Example: Check out the complete working example of state interpolation in the Tools and State Management example which demonstrates:

  • Using {cartCount} and {cart} in instructions
  • Tools updating state with context.state.set()
  • Instructions automatically reflecting current state
  • Agents referencing state without calling tools

This example shows exactly how the {variable_name} syntax works in practice with a shopping cart scenario.

Conditional Logic Instructions

Instructions that include decision trees and conditional behavior:

const diagnosticAgent = new LlmAgent({
  name: "technical_diagnostics",
  instruction: `
    You are a technical diagnostic specialist for troubleshooting system issues.

    **Diagnostic workflow:**

    IF issue_category == "performance":
      1. Check system_metrics for CPU, memory, disk usage
      2. Review recent_logs for error patterns
      3. If metrics > 80% → Recommend resource upgrade
      4. If metrics normal → Check application-specific logs

    IF issue_category == "connectivity":
      1. Run network_test to check connection status
      2. If internal network fails → Check local configuration
      3. If external network fails → Check firewall and DNS
      4. If partial connectivity → Run traceroute analysis

    IF issue_category == "security":
      1. Immediately run security_scan
      2. If threats detected → Transfer to security_specialist IMMEDIATELY
      3. If no threats → Check access_logs for unusual patterns
      4. Document all findings in security_report

    **Escalation rules:**
    - Security threats → security_specialist (immediate)
    - Hardware failures → hardware_support
    - Complex network issues → network_specialist
    - Database problems → database_admin

    **Communication:**
    - Explain each diagnostic step to the user
    - Provide ETA for resolution when possible
    - Always confirm before making system changes

    Use run_diagnostic tool for automated checks, log_analysis for reviewing system logs.
  `,
});

Instructions for Multi-Agent Systems

In multi-agent systems, instructions become even more critical for coordination:

// Parent agent with coordination instructions
const projectManagerAgent = new LlmAgent({
  name: "project_manager",
  globalInstruction:
    "All agents must provide progress updates and ask for clarification when requirements are unclear.",
  instruction: `
    You are a senior project manager coordinating a development team.

    **Team coordination principles:**

    **Planning phase:**
    1. Break down requests into clear, actionable tasks
    2. Assign tasks based on agent expertise and current workload
    3. Set realistic timelines with buffer for quality review
    4. Ensure all agents understand the overall project context

    **Execution phase:**
    1. Monitor progress and provide support when agents get stuck
    2. Facilitate communication between agents when coordination is needed
    3. Make decisions quickly when agents request clarification
    4. Adjust timelines and resources as issues arise

    **Quality assurance:**
    1. Always have code_reviewer check technical implementations
    2. Have content_editor review all user-facing text
    3. Run final integration tests before marking tasks complete
    4. Document lessons learned for future projects

    **Agent delegation patterns:**
    - requirements_analyst: For clarifying project scope and requirements
    - backend_developer: For API development and database work
    - frontend_developer: For UI/UX implementation
    - code_reviewer: For code quality and security review
    - content_editor: For documentation and user-facing text

    Keep the user informed of progress and any decisions made on their behalf.
  `,
  subAgents: [
    requirementsAnalyst,
    backendDev,
    frontendDev,
    codeReviewer,
    contentEditor,
  ],
});

// Specialized sub-agent with focused instructions
const backendDeveloper = new LlmAgent({
  name: "backend_developer",
  instruction: `
    You are a senior backend developer specializing in scalable API development.

    **Technical standards:**
    - Use TypeScript for all new code
    - Follow RESTful API design principles
    - Implement proper error handling and validation
    - Write comprehensive tests (minimum 80% coverage)
    - Document all API endpoints with OpenAPI specs

    **Architecture patterns:**
    - Use dependency injection for better testability
    - Implement repository pattern for data access
    - Use middleware for cross-cutting concerns (auth, logging, etc.)
    - Follow SOLID principles and clean architecture

    **Communication with team:**
    - Update project_manager on progress every 2 hours
    - Ask code_reviewer for feedback before finalizing implementations
    - Coordinate with frontend_developer on API contract changes
    - Flag any blocking issues immediately

    **Tools and workflow:**
    - Use code_generator for boilerplate API endpoints
    - Use test_runner to validate implementations
    - Use database_migrator for schema changes
    - Use api_docs_generator for documentation

    Always provide working, tested code with clear documentation.
  `,
});

Common Pitfalls to Avoid

Instruction Pitfalls

Too Vague: "Be helpful and answer questions"

  • Problem: No guidance on approach, tools, or boundaries
  • Solution: Define specific behaviors and decision criteria

Conflicting Directives: "Be concise but provide comprehensive details"

  • Problem: Contradictory requirements cause inconsistent behavior
  • Solution: Clarify priority and context for when to be brief vs detailed

Missing Context: "Transfer complex requests to the specialist"

  • Problem: Agent can't determine what constitutes "complex"
  • Solution: Define specific criteria for complexity and transfers

Overwhelming Length: 2000+ word instructions with every possible scenario

  • Problem: Important guidelines get lost in noise
  • Solution: Focus on core behaviors, use examples over exhaustive rules

Quick Reference Checklist

Use this checklist when writing agent instructions:

✅ Essential Elements

  • Role definition - What is the agent's primary function?
  • Scope and boundaries - What should/shouldn't the agent handle?
  • Tool usage guidelines - When and how to use each tool?
  • Communication style - How should the agent interact with users?
  • Decision criteria - How should the agent make choices?

✅ Multi-Agent Systems

  • Delegation rules - When to transfer to other agents?
  • Coordination patterns - How to work with other agents?
  • Escalation procedures - When to escalate or get help?
  • Context sharing - What information to pass between agents?
  • Quality standards - What constitutes good work?

✅ Advanced Features

  • Dynamic adaptation - How to adjust based on context?
  • Error handling - What to do when things go wrong?
  • Performance optimization - How to work efficiently?
  • Feedback loops - How to improve over time?
  • Monitoring hooks - What to log or report?

How is this guide?