Skip to content

AgentFactories

Rasmus Wulff Jensen edited this page Dec 25, 2025 · 18 revisions

Note

In the sample below, we create and use an OpenAIAgentFactory, but all factories works the same (but with slight variations)

(AnthropicAgentFactory, AzureOpenAIAgentFactory, GitHubAgentFactory, GoogleAgentFactory, MistralAgentFactory, OpenAIAgentFactory, OpenRouterAgentFactory, OpenAIAgentFactory)

Each AgentFactory serves the same purpose of creating Agent Definitions for you to use to call the LLMs.

Create the Agent Factory

You have 2 ways to create an Agent Factory (using simplified connection info or a Connection instance)

Option 1. Create a new instance manually

OpenAIAgentFactory agentFactory1 = new OpenAIAgentFactory("<api-key>");

//or

OpenAIAgentFactory agentFactory2 = new OpenAIAgentFactory(new OpenAIConnection
{
   ApiKey = "<api-key>",
   NetworkTimeout = TimeSpan.FromMinutes(5)
});

Option 2. Use Dependency Injection

builder.Services.AddOpenAIAgentFactory("<api-key");

//or

builder.Services.AddOpenAIAgentFactory(new OpenAIConnection
{
    ApiKey = "<api-key>",
    NetworkTimeout = TimeSpan.FromMinutes(5)
});

Using AgentFactory to create Agents

Each AgentFactory has 2 CreateAgent methods; one with simplified options, and one with more advanced options

//Create your simplified Agent (Support 'model', 'instructions', 'name' and 'tools')
OpenAIAgent agent = agentFactory.CreateAgent(model: "gpt-5", instructions: "You are a nice AI");

//Create your more advanced Agent with access to all options
OpenAIAgent agent = agentFactory.CreateAgent(new AgentOptions
{
    Model = "gpt-5",
    ReasoningEffort = OpenAIReasoningEffort.Low, //Set reasoning effort
    Instructions = "You are a nice AI", //The System Prompt
    Tools = [], //Add your tools here
});

Options you can provide while creating an Agent

Mandatory options

Mandatory Property Required by Notes
Model All - In AzureOpenAI, you technically provide the 'DeploymentName' from https://ai.azure.com and not the Model name
- Several of the Providers have a 'const' collection of Model-name, example OpenAIChatModels
MaxOutputTokens (Conditionally) Anthropic Only Anthropic has this requirement, and the rest can optionally set this

Commonly used options

Less-used options

Clone this wiki locally