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 method overloads: 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

In all AgentFactorries Model is mandatory; in AnthropicAgentFactory, the property MaxOutputTokens is also mandatory to set, while it is optional for the rest.

Property Notes
Model - 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 Only Anthropic has this requirement, and the rest can optionally set this.

Common Options

Most Agents needs Instructions and Tools to be of true value. Beyond that the various 'Thinking settings' for reasoning models are common to set and on OpenAI-based providers you can choose ClientType. Finally some scenario require you to specifiy a name for your Agent

Property Notes
Instructions Instructions is the LLMs System Message (sometimes also called the Developer Message). It allows you to steer the models, tone, rules and behaviour through Prompt Engeneering
Tools You can on an Agent set a collection of tools that Agent can choose to activate during a prompt (You often use the Instructions to steer the model on when and in what order tools should be called)
- Tip: You can use AIToolsFactory to help define your Tools beyond the normal AIFunctionFactory
ClientType In OpenAI-based systems (OpenAI, ÀzureOpenAI, OpenRouter and XAI) you can choose for each agent if you wish to use OpenAI's ChatClient protocol or their ResponseAPI protocol. By default an Agent use the ClientType defined on the parent Connection which have ChatClient as default, but you can override the default at connection level or directly on Agent creation using this property
'Thinking Settings' Across the various providers there is different ways to set how much an LLM should 'think' before answering
- On OpenAI-based Agents you can set the ReasoningEffort to none, minimal, low, medium, high or xhigh
- On OpenAI-based Agent using the ResponsesAPI you can set the ReasoningSummaryVerbosity for what reasoning summary should be returned (aka text on what the model 'thought' about)
- On AnthropicAgents you can set the BudgetTokens property to indicate how many tokens the Agent are allowed to think (minimum 1024 tokens)
- On GoogleAgents you can set the ThinkingBudget property to indicate how many tokens the Agent are allowed to think
Name The Name of the Agent (Optional in most cases, but some scenarios do require one)

Clone this wiki locally