Rediger

Step 1: Your First Agent

Create an agent and get a response — in just a few lines of code.

dotnet add package Azure.AI.Projects --prerelease
dotnet add package Azure.Identity
dotnet add package Microsoft.Agents.AI.Foundry --prerelease

Create the agent:

using System;
using Azure.AI.Projects;
using Azure.Identity;
using Microsoft.Agents.AI;

var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT")
    ?? throw new InvalidOperationException("Set AZURE_OPENAI_ENDPOINT");
var deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME") ?? "gpt-4o-mini";

AIAgent agent = new AIProjectClient(new Uri(endpoint), new DefaultAzureCredential())
    .AsAIAgent(
        model: deploymentName,
        instructions: "You are a friendly assistant. Keep your answers brief.",
        name: "HelloAgent");

Warning

DefaultAzureCredential is convenient for development but requires careful consideration in production. In production, consider using a specific credential (e.g., ManagedIdentityCredential) to avoid latency issues, unintended credential probing, and potential security risks from fallback mechanisms.

Run it:

Console.WriteLine(await agent.RunAsync("What is the largest city in France?"));

Or stream the response:

await foreach (var update in agent.RunStreamingAsync("Tell me a one-sentence fun fact."))
{
    Console.Write(update);
}

Tip

See here for a full runnable sample application.

pip install agent-framework azure-identity

Create and run an agent:

client = FoundryChatClient(
    project_endpoint="https://your-project.services.ai.azure.com",
    model="gpt-4o",
    credential=AzureCliCredential(),
)

agent = Agent(
    client=client,
    name="HelloAgent",
    instructions="You are a friendly assistant. Keep your answers brief.",
)
# Non-streaming: get the complete response at once
result = await agent.run("What is the capital of France?")
print(f"Agent: {result}")

Or stream the response:

# Streaming: receive tokens as they are generated
print("Agent (streaming): ", end="", flush=True)
async for chunk in agent.run("Tell me a one-sentence fun fact.", stream=True):
    if chunk.text:
        print(chunk.text, end="", flush=True)
print()

Note

Agent Framework does not automatically load .env files. To use a .env file for configuration, call load_dotenv() at the start of your script:

from dotenv import load_dotenv
load_dotenv()

Alternatively, set environment variables directly in your shell or IDE. See the settings migration note for details.

Tip

See the full sample for the complete runnable file.

go get github.com/microsoft/agent-framework-go

Create the agent:

package main

import (
    "context"
    "fmt"
    "os"

    "github.com/microsoft/agent-framework-go/agent"
    "github.com/microsoft/agent-framework-go/provider/foundryprovider"

    "github.com/Azure/azure-sdk-for-go/sdk/azidentity"
)

func main() {
    endpoint := os.Getenv("FOUNDRY_PROJECT_ENDPOINT")
    model := os.Getenv("FOUNDRY_MODEL")

    token, err := azidentity.NewDefaultAzureCredential(nil)
    if err != nil {
        panic(err)
    }

    a := foundryprovider.NewAgent(
        endpoint,
        token,
        foundryprovider.ModelDeployment(model),
        foundryprovider.AgentConfig{
            Instructions: "You are a friendly assistant. Keep your answers brief.",
            Config: agent.Config{
                Name: "HelloAgent",
            },
        },
    )

Warning

azidentity.NewDefaultAzureCredential is convenient for development but requires careful consideration in production. In production, consider using a specific credential, such as azidentity.NewManagedIdentityCredential, to avoid latency issues, unintended credential probing, and potential security risks from fallback mechanisms.

Run it:

    ctx := context.Background()

    resp, err := a.RunText(ctx, "What is the largest city in France?").Collect()
    fmt.Println(resp, err)

Or stream the response:

    for update, err := range a.RunText(ctx, "Tell me a one-sentence fun fact.", agent.Stream(true)) {
        if err != nil {
            panic(err)
        }
        fmt.Print(update)
    }
}

Tip

See the full sample for the complete runnable file.

Next steps

Go deeper: