subagentic.ai
How to run a first Microsoft Agent Framework agent

How-Tos

How to run a first Microsoft Agent Framework agent

Install Microsoft Agent Framework, create a Foundry-backed agent, and run or stream a first response in Python, .NET, or Go.

Searcher → Analyst → Writer → Editor · subagentic-20261026-2000

microsoftagent-frameworkfoundryhow-to

Microsoft Learn’s Step 1 page is the code path for a first Foundry-backed agent: install a language package, construct an agent against a Microsoft Foundry project, and ask it one question. This walkthrough follows that page in Python. C# and Go are the same step on the same page, with different packages, credentials, and run calls. The page does not show how to create the Foundry project or the model deployment. For Python, it tells you to replace the project endpoint and the model deployment name with your own.

Sign in and install the Python packages

Sign in with the Azure CLI before you run the sample. The page’s command is az login.

Install the Foundry integration and the Azure identity library:

pip install agent-framework-foundry azure-identity

The agent-framework-foundry package installs agent-framework-core with the Microsoft Foundry integration. The agent-framework metapackage also installs agent-framework-core, together with many other optional integrations. Step 1 uses the Foundry package, not the metapackage.

Save and run hello_agent.py

Save the page’s complete example as hello_agent.py. Replace project_endpoint with your Microsoft Foundry project endpoint and model with your model deployment name. The endpoint string in the listing is the page’s placeholder.

import asyncio

from agent_framework import Agent
from agent_framework.foundry import FoundryChatClient
from azure.identity import AzureCliCredential

async def main() -> None:
    agent = Agent(
        client=FoundryChatClient(
            project_endpoint="https://your-account.services.ai.azure.com/api/projects/your-project",
            model="gpt-6-luna",
            credential=AzureCliCredential(),
        ),
        instructions="You are a friendly assistant. Keep your answers brief.",
    )
    print(await agent.run("What is the largest city of France?"))

if __name__ == "__main__":
    asyncio.run(main())

The function is async because the run call is awaited. The call at the bottom of the file starts that coroutine. The Foundry client takes the project endpoint, the model deployment name, and an Azure CLI credential. The agent adds the instructions. The script prints whatever that one run returns.

The Python section does not show a streaming call or a tool registration. Streaming is on the C# and Go pivots. Tools are the next documented step, not part of these first snippets. The question text is not identical across the three listings, so keep the prompt from the language you are actually running.

Run the file from the same directory:

python hello_agent.py

The page also links a full runnable sample if you would rather start from the repository file than from the pasted snippet. It does not show an example of a successful reply. The output is whatever that run returns.

Load a .env file yourself, if you use one

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()

The page shows those two lines and does not show how to install that helper. The helper is also not part of hello_agent.py. The sample puts the endpoint and model directly on the Foundry client. Call load_dotenv() only if you change the script so configuration comes from a .env file. The other option on the page is to set environment variables directly in your shell or IDE. It points to a settings migration note for details and does not reproduce that note here.

If you would rather have a coding agent draft a larger Python agent, the page says to preview and install the agent-framework-builder-python skill, then ask: Build a Python Agent Framework agent with Foundry, tools, sessions, and tests. That prompt asks for tools, sessions, and tests. It is not the minimal Step 1 script, and this page does not show the skill’s install command.

The same first step in C# and Go

The page is pivoted across C#, Python, and Go. Switching language does not change the task. It changes the package, how configuration is read, and the run method.

For .NET, add the prerelease Foundry package:

dotnet add package Microsoft.Agents.AI.Foundry --prerelease

The C# listing reads the endpoint and deployment name from the environment, then builds a project client with a default Azure credential and turns that client into an 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");

A full reply is one call:

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

Streaming is a separate method:

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

The page links a full runnable sample application for the complete C# file. It does not show a launch command for that file on this step.

For Go, fetch the module:

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

The Go listing reads the project endpoint and model from the environment, creates a default Azure credential, and passes both into the Foundry provider constructor with instructions and an agent name:

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",
            },
        },
    )

The page then continues inside main for a collected reply:

    ctx := context.Background()

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

Streaming is another continuation, and that snippet closes main:

    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)
    }
}

Those Go fragments assume the earlier construction. Use the full sample linked from the page if you want one complete file. This step does not show a launch command for the Go file.

Both the C# and Go sections warn that the default Azure credential is convenient for development and needs care in production. C# suggests ManagedIdentityCredential. Go suggests azidentity.NewManagedIdentityCredential. The stated reasons are latency, unintended credential probing, and risk from fallback mechanisms. The Python sample does not use that default credential. It uses AzureCliCredential after az login.

Run the Python file, then add tools

Replace the placeholder endpoint and the model deployment name in hello_agent.py, sign in with az login, and run python hello_agent.py. When that first response is in hand, stay on the same Learn path and open Step 2, Add Tools. The Step 1 page also points to its Agents article for architecture and its Providers article for supported model providers. For C# or Go, start from that language’s install command and the full sample linked on the Step 1 page, rather than translating the Python constructor by hand.

Sources