Skip to main content

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

Environment variables keep secrets out of source code and provide flexibility across environments.
3

Create the agent

The fluent API chains: Authentication → Chat Client → AI Agent
4

Run the agent

RunAsync sends a message and returns the complete response as a string.

Run the Agent

You should see a pirate joke printed to the console!

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

Key points:
  • 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:
  1. Recognizes it needs weather information
  2. Calls the GetWeather function with location: "Amsterdam"
  3. 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

Use AgentSession to maintain conversation context:
Program.cs

Session Management

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

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

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 tools parameter when creating the agent
  • Verify the agent’s instructions mention it should use tools if needed
Problem: No output when using RunStreamingAsync.Solutions:
  • Use Console.Write() not Console.WriteLine() for each update
  • Ensure you’re using await foreach correctly
  • Check network connectivity and API quotas
Problem: Agent doesn’t remember previous messages.Solutions:
  • Ensure you’re passing the same session object to each RunAsync call
  • Verify you created the session with await agent.CreateSessionAsync()
  • Check that you’re not creating a new session between turns
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