02 · Providers · OpenAI

The provider is the swappable back end: the same Joker agent, one-shot and streaming, now through the OpenAI API.

Part 57 of 91 Microsoft Agent Framework Go — Every Lesson

What this lesson demonstrates

Everything in module 01 ran on Azure AI Foundry. But an agent.Agent doesn’t care who runs the model — that is the provider’s job. This lesson rebuilds the exact same “Joker” agent, both one-shot and streaming, but through openaiprovider talking to the OpenAI API. Notice how little changes: the constructor and the credentials, and nothing else.

The wiring

The agent is built from an openai.Client and an openaiprovider.AgentConfig that names the model directly:

func newJoker(client openai.Client, model string, mw agent.Middleware) *agent.Agent {
    return openaiprovider.NewAgent(
        client,
        openaiprovider.AgentConfig{
            Model:        model,
            Instructions: "You are good at telling jokes.",
            Config: agent.Config{
                Name:        "Joker",
                Middlewares: []agent.Middleware{mw}, // wraps every run, for logging
            },
        },
    )
}

main runs the prompt twice — first RunText(...).Collect() to get the whole response at once, then the same call with agent.Stream(true) to range over incremental updates. The defaultModel is gpt-4o-mini.

What to notice

How it maps to the Microsoft Agent Framework

The provider is the whole portability story of the Agent Framework Go SDK. openaiprovider.NewAgent(...) returns the same *agent.Agent that foundryprovider.NewAgent(...) did — so RunText, .Collect(), and streaming are unchanged when you move a workload between OpenAI and Azure AI Foundry. Even swapping NewAgent for NewChatCompletionsAgent (Chat Completions instead of Responses) leaves your call sites and tests untouched.

Run it

OPENAI_API_KEY=sk-... go run ./tutorial/02-agents/providers/openai. The offline test (wiring + middleware) needs no key; TestOpenAI_Live makes the real model call and is gated behind AF_LIVE=1.


Next: step01 · File-Based Skills

Sources & References

The Go SDK this lesson exercises
Provider portability across OpenAI and Azure AI Foundry