Overview
In this guide, you’ll build a fully-featured AI agent step-by-step. You’ll learn how to:- Create an agent with custom instructions
- Add function tools for extended capabilities
- Implement streaming responses
- Manage multi-turn conversations with sessions
- Handle errors and edge cases
This guide assumes you’ve completed the Installation guide and reviewed the Quickstart.
Project Setup
First, create a new console application:Step 1: Hello Agent
Let’s start with a simple agent that tells jokes:Program.cs
Understanding the Code
1
Import namespaces
2
Configure connection
3
Create the agent
4
Run the agent
RunAsync sends a message and returns the complete response as a string.Run the Agent
Step 2: Add Streaming
Streaming provides real-time feedback as the agent generates responses:Program.cs
Streaming Explained
Use streaming for user-facing applications to provide immediate feedback. Use non-streaming for background processing or when you need the complete response before proceeding.
Step 3: Add Function Tools
Tools allow agents to call functions and access external data:Program.cs
Tool Development Guide
1
Define the function
- Use
[Description]attribute on the function and parameters - Descriptions help the agent understand when to call the function
- Keep function names and descriptions clear and specific
2
Register the tool
AIFunctionFactory.Create() converts your function into an AIFunction that the agent can call.3
The agent decides when to call
When you ask “What is the weather like in Amsterdam?”, the agent:
- Recognizes it needs weather information
- Calls the
GetWeatherfunction withlocation: "Amsterdam" - Incorporates the result into its response
Advanced Tool Example
Here’s a more complex tool with multiple parameters:The agent can call multiple tools in a single turn to gather all necessary information.
Step 4: Multi-Turn Conversations
UseAgentSession to maintain conversation context:
Program.cs
Session Management
When to use sessions
When to use sessions
Use sessions when:
- Building conversational interfaces (chatbots, assistants)
- The agent needs to remember previous interactions
- You’re implementing follow-up questions or clarifications
- Context from earlier messages affects later responses
Session lifecycle
Session lifecycle
Multiple concurrent sessions
Multiple concurrent sessions
Streaming with Sessions
You can combine sessions with streaming:Complete Example: Weather Assistant
Here’s a complete, production-ready weather assistant:Program.cs
Try These Queries
Run the application and try:- “What’s the weather in Seattle?”
- “How about Tokyo?”
- “Give me the forecast for Amsterdam”
- “Compare the weather in London and Tokyo”
Error Handling
Add robust error handling to your agents:Best Practices
Clear Instructions
Provide specific, detailed instructions to guide agent behavior.
Tool Descriptions
Write clear descriptions for functions and parameters.
Use Async/Await
Always use async patterns; never block with
.Result or .Wait().Handle Errors
Implement proper error handling for network and API failures.
Environment Config
Use environment variables for all configuration and secrets.
Session Management
Create sessions for conversations, dispose when no longer needed.
Next Steps
Working with Tools
Deep dive into creating and using agent tools
Agent Sessions
Advanced session management and context control
Memory & Context
Add persistent memory to your agents
Workflows
Build multi-agent workflows and orchestration
Troubleshooting
Agent not calling tools
Agent not calling tools
Problem: Agent ignores available functions.Solutions:
- Ensure
[Description]attributes are clear and specific - Check that tool parameters match what the agent expects
- Make sure you’re passing
toolsparameter when creating the agent - Verify the agent’s instructions mention it should use tools if needed
Streaming not working
Streaming not working
Problem: No output when using
RunStreamingAsync.Solutions:- Use
Console.Write()notConsole.WriteLine()for each update - Ensure you’re using
await foreachcorrectly - Check network connectivity and API quotas
Session context not preserved
Session context not preserved
Problem: Agent doesn’t remember previous messages.Solutions:
- Ensure you’re passing the same
sessionobject to eachRunAsynccall - Verify you created the session with
await agent.CreateSessionAsync() - Check that you’re not creating a new session between turns
Performance issues
Performance issues
Problem: Agent responses are slow.Solutions:
- Use streaming for better perceived performance
- Consider using a faster model (gpt-4o-mini vs gpt-4)
- Reduce the number of tools if you have many
- Check your network latency to Azure