AxCrew

Working with Examples

Learn how to use examples to improve your AxCrew agents

Understanding Examples

Examples are input/output pairs that help guide your agent's behavior. They work as few-shot learning examples, showing the agent how to respond to specific inputs.

Benefits of using examples:

  • Achieve more consistent output formats
  • Guide the agent toward specific response styles
  • Demonstrate step-by-step problem-solving approaches
  • Teach agents how to handle edge cases

Adding Examples to Agent Configuration

You can add examples to your agent configuration using the examples array:

{
  "name": "MathTeacher",
  "description": "Solves math problems with step by step explanations",
  "signature": "problem:string -> solution:string",
  "provider": "google-gemini",
  "providerKeyName": "GEMINI_API_KEY",
  "ai": {
    "model": "gemini-1.5-pro",
    "temperature": 0
  },
  "examples": [
    {
      "problem": "what is the square root of 144?",
      "solution": "Let's solve this step by step:\n1. The square root of a number is a value that, when multiplied by itself, gives the original number\n2. For 144, we need to find a number that when multiplied by itself equals 144\n3. 12 × 12 = 144\nTherefore, the square root of 144 is 12"
    },
    {
      "problem": "what is the cube root of 27?",
      "solution": "Let's solve this step by step:\n1. The cube root of a number is a value that, when multiplied by itself twice, gives the original number\n2. For 27, we need to find a number that when cubed equals 27\n3. 3 × 3 × 3 = 27\nTherefore, the cube root of 27 is 3"
    }
  ]
}

Matching Examples to Signatures

For examples to be effective, they must match the input/output signature of your agent:

  1. Field names in the examples should exactly match those in your agent's signature
  2. Field values should reflect the expected types (strings, numbers, objects, etc.)
  3. The format and style of the examples should demonstrate how you want the agent to respond

For an agent with signature:

query:string, maxResults:number -> results:string[]

Your examples should look like:

{
  "examples": [
    {
      "query": "best hiking spots in California",
      "maxResults": 3,
      "results": ["Yosemite Valley", "Joshua Tree", "Big Sur"]
    }
  ]
}

Example Quality Guidelines

For the most effective examples:

  1. Be Specific: Include details that guide the agent's understanding
  2. Use Realistic Data: Examples should reflect real-world scenarios
  3. Cover Edge Cases: Include examples that handle unusual inputs
  4. Consistent Formatting: Maintain a consistent format across all examples
  5. Include Reasoning: For complex tasks, show the reasoning process
  6. Appropriate Length: Examples should be concise but complete
  7. Diverse Examples: Include a range of different input scenarios

Example Types

Format Examples

Demonstrate the desired output format:

{
  "examples": [
    {
      "topic": "climate change",
      "outline": "# Climate Change\n\n## Introduction\n- Definition\n\n## Causes\n- Greenhouse gases\n\n## Solutions\n- Renewable energy"
    }
  ]
}

Reasoning Examples

Demonstrate the reasoning process:

{
  "examples": [
    {
      "problem": "If a train travels at 60 mph, how long to travel 150 miles?",
      "solution": "Time = Distance ÷ Speed\nTime = 150 ÷ 60 = 2.5 hours"
    }
  ]
}

Edge Case Examples

Show how to handle unusual inputs:

{
  "examples": [
    {
      "query": "",
      "response": "Please provide a search query."
    },
    {
      "query": "asdfghjkl",
      "response": "Your query doesn't appear to be recognizable. Could you clarify?"
    }
  ]
}

Example Count Recommendations

  • Minimum: 2-3 examples to show patterns
  • Optimal: 3-5 examples for most use cases
  • Complex Tasks: 5-10 examples for more complex tasks
  • Balance: Too few won't guide the agent enough; too many might consume excessive tokens

Dynamic Examples

You can also generate examples dynamically:

import { AxCrew } from '@amitdeshmukh/ax-crew';
 
const baseConfig = {
  crew: [{
    name: "ArticleWriter",
    description: "Writes articles on various topics",
    signature: "topic:string, style:string -> article:string",
    provider: "openai",
    providerKeyName: "OPENAI_API_KEY",
    ai: { model: "gpt-4", temperature: 0.7 }
  }]
};
 
// Add examples dynamically
baseConfig.crew[0].examples = [
  { topic: "Renewable Energy", style: "academic", article: "..." },
  { topic: "Machine Learning", style: "beginner-friendly", article: "..." }
];
 
const crew = new AxCrew(baseConfig);
await crew.addAllAgents();

Best Practices

  1. Match Your Use Case: Tailor examples to your specific use case
  2. Regular Updates: Refine examples based on actual outputs
  3. Balance Specificity: Be specific enough to guide but not so rigid that the agent can't generalize
  4. Consistent Formatting: Maintain the same format across examples
  5. Test Thoroughly: Test with various inputs to ensure examples are effective
  6. Adjust Temperature: Lower temperature (0-0.3) makes agents follow examples more closely

On this page