agentleFS
MicrosoftKnown publishermicrosoft/AZD-for-beginners/translations/en/docs/chapter-02-ai-development/agents.md

AZD-for-beginners / chapter-02-ai-development

AGENTS.md190 starsChanged 5 months agoMITView on GitHub

Chapter Navigation: - πŸ“š Course Home: AZD For Beginners - πŸ“– Current Chapter: Chapter 2 - AI-First Development - ⬅️ Previous: Microsoft Foundry Integration - ➑️ Next: AI Model Deployment - πŸš€ Advanced: Multi-Agent Solutions AI agents are autonomous programs that can perceive their environment, make decisions, and take actions to achieve specific goals. Unlike simple chatbots that respond to prompts, agents can: This guide shows you how to deploy AI agents to Azure using Azure Developer CLI (azd). Validation…

  • Installs packages

The file

We split this agents.md into the rules it gives an agent

7 rules. About 0 are common, 5 are distinctive, and 2 are very specific to this project.

  • β‰ˆ0

    Common Β· estimate

    Many files like this have a rule of this kind.

  • β‰ˆ5

    Distinctive Β· estimate

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Use tools: - Call APIs, search databases, execute code

    line 16
  2. Very specific:

    If you have an agent manifest file, you can use the azd ai command to scaffold a Foundry Agent Service project directly.

    line 124
  3. Very specific:

    After azd up, the same extension carries you through the rest of the agent lifecycle: azd ai agent invoke to test, azd ai agent eval …

    line 152
  4. Distinctive:

    Document Q&A systems

    line 192
  5. Distinctive:

    All AZD agent templates include Application Insights for monitoring:

    line 272
  6. Distinctive:

    Create a new tool function in your agent code:

    line 503
  7. Distinctive:

    Goal: Create an agent that answers questions from your documents

    line 546

β‰ˆ β€œCommon” is an estimate: at least 1 in 5 of the 16,595 AGENTS.md files we read have a rule of the same kind, not the same words. β€œVery specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
agents.md

AI Agents with Azure Developer CLI

Chapter Navigation:

  • πŸ“š Course Home: AZD For Beginners
  • πŸ“– Current Chapter: Chapter 2 - AI-First Development
  • ⬅️ Previous: Microsoft Foundry Integration
  • ➑️ Next: AI Model Deployment
  • πŸš€ Advanced: Multi-Agent Solutions

Introduction

AI agents are autonomous programs that can perceive their environment, make decisions, and take actions to achieve specific goals. Unlike simple chatbots that respond to prompts, agents can:

  • Use tools - Call APIs, search databases, execute code
  • Plan and reason - Break complex tasks into steps
  • Learn from context - Maintain memory and adapt behavior
  • Collaborate - Work with other agents (multi-agent systems)

This guide shows you how to deploy AI agents to Azure using Azure Developer CLI (azd).

Validation note (2026-07-13): This guide was reviewed against azd 1.27.1 and azure.ai.agents 1.0.0-beta.5. The azd ai experience is still preview-driven, so check extension help if your installed flags differ.

Learning Goals

By completing this guide, you will:

  • Understand what AI agents are and how they differ from chatbots
  • Deploy pre-built AI agent templates using AZD
  • Configure Foundry Agents for custom agents
  • Implement basic agent patterns (tool use, RAG, multi-agent)
  • Monitor and debug deployed agents

Learning Outcomes

Upon completion, you will be able to:

  • Deploy AI agent applications to Azure with a single command
  • Configure agent tools and capabilities
  • Implement retrieval-augmented generation (RAG) with agents
  • Design multi-agent architectures for complex workflows
  • Troubleshoot common agent deployment issues

πŸ€– What Makes an Agent Different from a Chatbot?

FeatureChatbotAI Agent
BehaviorResponds to promptsTakes autonomous actions
ToolsNoneCan call APIs, search, execute code
MemorySession-based onlyPersistent memory across sessions
PlanningSingle responseMulti-step reasoning
CollaborationSingle entityCan work with other agents

Simple Analogy

  • Chatbot = A helpful person answering questions at an information desk
  • AI Agent = A personal assistant who can make calls, book appointments, and complete tasks for you

πŸš€ Quick Start: Deploy Your First Agent

Option 1: Foundry Agents Template (Recommended)

# Initialize the AI agents template
azd init --template get-started-with-ai-agents

# Deploy to Azure
azd up

What gets deployed:

  • βœ… Foundry Agents
  • βœ… Microsoft Foundry Models (gpt-4.1)
  • βœ… Azure AI Search (for RAG)
  • βœ… Azure Container Apps (web interface)
  • βœ… Application Insights (monitoring)

Time: ~15-20 minutes Cost: ~$100-150/month (development)

Option 2: OpenAI Agent with Prompty

# Initialize the Prompty-based agent template
azd init --template agent-openai-python-prompty

# Deploy to Azure
azd up

What gets deployed:

  • βœ… Azure Functions (serverless agent execution)
  • βœ… Microsoft Foundry Models
  • βœ… Prompty configuration files
  • βœ… Sample agent implementation

Time: ~10-15 minutes Cost: ~$50-100/month (development)

Option 3: RAG Chat Agent

# Initialize RAG chat template
azd init --template azure-search-openai-demo

# Deploy to Azure
azd up

What gets deployed:

  • βœ… Microsoft Foundry Models
  • βœ… Azure AI Search with sample data
  • βœ… Document processing pipeline
  • βœ… Chat interface with citations

Time: ~15-25 minutes Cost: ~$80-150/month (development)

Option 4: AZD AI Agent Init (Manifest- or Template-Based Preview)

If you have an agent manifest file, you can use the azd ai command to scaffold a Foundry Agent Service project directly. Recent preview releases also added template-based initialization support, so the exact prompt flow may differ slightly depending on your installed extension version.

# Install the AI agents extension
azd extension install azure.ai.agents

# Optional: verify the installed preview version
azd extension show azure.ai.agents

# Initialize from an agent manifest
azd ai agent init -m agent-manifest.yaml

# Deploy to Azure
azd up

# Test the deployed agent (shows latency + time-to-first-byte)
azd ai agent invoke

When to use azd ai agent init vs azd init --template:

ApproachBest ForHow It Works
azd init --templateStarting from a working sample appClones a full template repo with code + infra
azd ai agent init -mBuilding from your own agent manifestScaffolds project structure from your agent definition

Tip: Use azd init --template when learning (Options 1-3 above). Use azd ai agent init when building production agents with your own manifests.

After azd up, the same extension carries you through the rest of the agent lifecycle: azd ai agent invoke to test, azd ai agent eval generate and azd ai agent optimize to measure and improve quality, and azd ai agent delete to clean up. See AZD AI CLI Commands for the full reference.


πŸ—οΈ Agent Architecture Patterns

Pattern 1: Single Agent with Tools

The simplest agent pattern - one agent that can use multiple tools.

graph TD
    UI[User Interface] --> Agent[AI Agent<br/>gpt-4.1]
    Agent --> Search[Search Tool]
    Agent --> Database[Database Tool]
    Agent --> API[API Tool]

Best for:

  • Customer support bots
  • Research assistants
  • Data analysis agents

AZD Template: azure-search-openai-demo

Pattern 2: RAG Agent (Retrieval-Augmented Generation)

An agent that retrieves relevant documents before generating responses.

graph TD
    Query[User Query] --> RAG[RAG Agent]
    RAG --> Vector[Vector Search]
    RAG --> LLM[LLM<br/>gpt-4.1]
    Vector -- Documents --> LLM
    LLM --> Response[Response with Citations]

Best for:

  • Enterprise knowledge bases
  • Document Q&A systems
  • Compliance and legal research

AZD Template: azure-search-openai-demo

Pattern 3: Multi-Agent System

Multiple specialized agents working together on complex tasks.

graph TD
    Orchestrator[Orchestrator Agent] --> Research[Research Agent<br/>gpt-4.1]
    Orchestrator --> Writer[Writer Agent<br/>gpt-4.1-mini]
    Orchestrator --> Reviewer[Reviewer Agent<br/>gpt-4.1]

Best for:

  • Complex content generation
  • Multi-step workflows
  • Tasks requiring different expertise

Learn More: Multi-Agent Coordination Patterns


βš™οΈ Configuring Agent Tools

Agents become powerful when they can use tools. Here's how to configure common tools:

Tool Configuration in Foundry Agents

# agent_config.py
from azure.ai.projects import AIProjectClient
from azure.ai.projects.models import FunctionTool, CodeInterpreterTool

# Define custom tools
search_tool = FunctionTool(
    name="search_knowledge_base",
    description="Search the company knowledge base for relevant documents",
    parameters={
        "type": "object",
        "properties": {
            "query": {
                "type": "string",
                "description": "The search query"
            }
        },
        "required": ["query"]
    }
)

# Create agent with tools
agent = project_client.agents.create_agent(
    model="gpt-4.1",
    name="Support Agent",
    instructions="You are a helpful support agent. Use the search tool to find relevant information.",
    tools=[search_tool, CodeInterpreterTool()]
)

Environment Configuration

# Set up agent-specific environment variables
azd env set AZURE_OPENAI_MODEL "gpt-4.1"
azd env set AGENT_INSTRUCTIONS "You are a helpful assistant..."
azd env set ENABLE_CODE_INTERPRETER "true"
azd env set ENABLE_FILE_SEARCH "true"

# Deploy with updated configuration
azd deploy

πŸ“Š Monitoring Agents

Application Insights Integration

All AZD agent templates include Application Insights for monitoring:

# Open monitoring dashboard
azd monitor --overview

# View live logs
azd monitor --logs

# View live metrics
azd monitor --live

Key Metrics to Track

MetricDescriptionTarget
Response LatencyTime to generate response< 5 seconds
Token UsageTokens per requestMonitor for cost
Tool Call Success Rate% of successful tool executions> 95%
Error RateFailed agent requests< 1%
User SatisfactionFeedback scores> 4.0/5.0

Custom Logging for Agents

import os
from azure.monitor.opentelemetry import configure_azure_monitor
from opentelemetry import trace

# Configure Azure Monitor with OpenTelemetry
configure_azure_monitor(
    connection_string=os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"]
)

tracer = trace.get_tracer(__name__)

def log_agent_interaction(user_query, agent_response, tools_used, latency_ms):
    with tracer.start_as_current_span("agent_interaction") as span:
        span.set_attributes({
            "user_query": user_query,
            "response_length": len(agent_response),
            "tools_used": tools_used,
            "latency_ms": latency_ms
        })

Note: Install the required packages: pip install azure-monitor-opentelemetry opentelemetry


πŸ’° Cost Considerations

Estimated Monthly Costs by Pattern

PatternDev EnvironmentProduction
Single Agent$50-100$200-500
RAG Agent$80-150$300-800
Multi-Agent (2-3 agents)$150-300$500-1,500
Enterprise Multi-Agent$300-500$1,500-5,000+

Cost Optimization Tips

  1. Use gpt-4.1-mini for simple tasks
    azd env set AZURE_OPENAI_MODEL "gpt-4.1-mini"
  2. Implement caching for repeated queries
    from functools import lru_cache
    
    @lru_cache(maxsize=1000)
    def get_cached_response(query_hash):
        return agent.run(query_hash)
  3. Set token limits per run
    # Set max_completion_tokens when running the agent, not during creation
    run = project_client.agents.create_run(
        thread_id=thread.id,
        agent_id=agent.id,
        max_completion_tokens=1000  # Limit response length
    )
  4. Scale to zero when not in use
    # Container Apps automatically scale to zero
    azd env set MIN_REPLICAS "0"

πŸ”§ Troubleshooting Agents

Common Issues and Solutions

<details> <summary><strong>❌ Agent not responding to tool calls</strong></summary>
# Check if tools are properly registered
azd show

# Verify OpenAI deployment
az cognitiveservices account deployment list \
  --name $AZURE_OPENAI_NAME \
  --resource-group $RG_NAME

# Check agent logs
azd monitor --logs

Common causes:

  • Tool function signature mismatch
  • Missing required permissions
  • API endpoint not accessible
</details><details> <summary><strong>❌ High latency in agent responses</strong></summary>
# Check Application Insights for bottlenecks
azd monitor --live

# Consider using a faster model
azd env set AZURE_OPENAI_MODEL "gpt-4.1-mini"
azd deploy

Optimization tips:

  • Use streaming responses
  • Implement response caching
  • Reduce context window size
</details><details> <summary><strong>❌ Agent returning incorrect or hallucinated information</strong></summary>
# Improve with better system prompts
instructions = """
You are a helpful assistant. IMPORTANT:
- Only answer based on provided context
- If you don't know, say "I don't know"
- Always cite your sources
- Never make up information
"""

# Add retrieval for grounding
agent = project_client.agents.create_agent(
    model="gpt-4.1",
    instructions=instructions,
    tools=[FileSearchTool()]  # Ground responses in documents
)
</details><details> <summary><strong>❌ Token limit exceeded errors</strong></summary>
# Implement context window management
def truncate_context(messages, max_tokens=8000, model="gpt-4.1"):
    """Keep only recent messages within token limit."""
    import tiktoken
    encoding = tiktoken.encoding_for_model(model)
    total_tokens = 0
    truncated = []
    
    for msg in reversed(messages):
        msg_tokens = len(encoding.encode(msg.content))
        if total_tokens + msg_tokens > max_tokens:
            break
        truncated.insert(0, msg)
        total_tokens += msg_tokens
    
    return truncated
</details>

πŸŽ“ Hands-On Exercises

Exercise 1: Deploy a Basic Agent (20 minutes)

Goal: Deploy your first AI agent using AZD

# Step 1: Initialize template
azd init --template get-started-with-ai-agents

# Step 2: Login to Azure
azd auth login
# If you work across tenants, add --tenant-id <tenant-id>

# Step 3: Deploy
azd up

# Step 4: Test the agent
# Expected output after deployment:
#   Deployment Complete!
#   Endpoint: https://<app-name>.<region>.azurecontainerapps.io
# Open the URL shown in the output and try asking a question

# Step 5: View monitoring
azd monitor --overview

# Step 6: Clean up
azd down --force --purge

Success Criteria:

  • Agent responds to questions
  • Can access monitoring dashboard via azd monitor
  • Resources cleaned up successfully

Exercise 2: Add a Custom Tool (30 minutes)

Goal: Extend an agent with a custom tool

  1. Deploy the agent template:
    azd init --template get-started-with-ai-agents
    azd up
  2. Create a new tool function in your agent code:
    def get_weather(location: str) -> str:
        """Get current weather for a location."""
        # API call to weather service
        return f"Weather in {location}: Sunny, 72Β°F"
  3. Register the tool with the agent:
    from azure.ai.projects.models import FunctionTool
    
    weather_tool = FunctionTool(
        name="get_weather",
        description="Get current weather for a location",
        parameters={
            "type": "object",
            "properties": {
                "location": {"type": "string", "description": "City name"}
            },
            "required": ["location"]
        }
    )
    
    agent = project_client.agents.create_agent(
        model="gpt-4.1",
        name="Weather Agent",
        tools=[weather_tool]
    )
  4. Redeploy and test:
    azd deploy
    # Ask: "What's the weather in Seattle?"
    # Expected: Agent calls get_weather("Seattle") and returns weather info

Success Criteria:

  • Agent recognizes weather-related queries
  • Tool is called correctly
  • Response includes weather information

Exercise 3: Build a RAG Agent (45 minutes)

Goal: Create an agent that answers questions from your documents

# Step 1: Deploy RAG template
azd init --template azure-search-openai-demo
azd up

# Step 2: Upload your documents
# Place PDF/TXT files in the data/ directory, then run:
python scripts/prepdocs.py

# Step 3: Test with domain-specific questions
# Open the web app URL from the azd up output
# Ask questions about your uploaded documents
# Responses should include citation references like [doc.pdf]

Success Criteria:

  • Agent answers from uploaded documents
  • Responses include citations
  • No hallucination on out-of-scope questions

πŸ“š Next Steps

Now that you understand AI agents, explore these advanced topics:

TopicDescriptionLink
Multi-Agent SystemsBuild systems with multiple collaborating agentsRetail Multi-Agent Example
Coordination PatternsLearn orchestration and communication patternsCoordination Patterns
Production DeploymentEnterprise-ready agent deploymentProduction AI Practices
Agent EvaluationTest and evaluate agent performanceAI Troubleshooting
AI Workshop LabHands-on: Make your AI solution AZD-readyAI Workshop Lab

πŸ“– Additional Resources

Official Documentation

AZD Templates for Agents

Community Resources

Agent Skills for Your Editor


Navigation

  • Previous Lesson: Microsoft Foundry Integration
  • Next Lesson: AI Model Deployment

<!-- CO-OP TRANSLATOR DISCLAIMER START -->

Disclaimer: This document has been translated using AI translation service Co-op Translator. While we strive for accuracy, please be aware that automated translations may contain errors or inaccuracies. The original document in its native language should be considered the authoritative source. For critical information, professional human translation is recommended. We are not liable for any misunderstandings or misinterpretations arising from the use of this translation.

<!-- CO-OP TRANSLATOR DISCLAIMER END -->

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.