Agent Context (IAgentContext) & Run Configuration (AgentRunConfig)
Two of the most critical elements controlling an agent's behavior in AgentB are the IAgentContext
and the AgentRunConfig
.
IAgentContext
: The Agent's Environment
IAgentContext
: The Agent's EnvironmentThe IAgentContext
is an object passed to an agent's run()
method (and other lifecycle methods like submitToolOutputs()
). It acts as a "service locator" or "dependency injection container" for a specific agent execution, providing the agent with everything it needs to operate.
Key Properties of IAgentContext
:
runId: string
: The unique identifier for the current agent run.threadId: string
: The identifier of the conversation thread this run belongs to.llmClient: ILLMClient
: An instance of an LLM client (e.g.,OpenAIAdapter
) used to make calls to the Large Language Model.toolProvider: IToolProvider
:Provides access to the tools available to the agent for this specific run.
For a
BaseAgent
ingenericOpenApi
mode, this might be anOpenAPIConnector
.For a
PlanningAgent
inhierarchicalPlanner
mode, this provider would primarily offer theDelegateToSpecialistTool
.For a "worker" agent invoked by the
DelegateToSpecialistTool
, this provider would offer only the tools from the specificIToolSet
assigned to that worker.
messageStorage: IMessageStorage
: Used to save new messages (user, assistant, tool) and retrieve conversation history.responseProcessor: LLMResponseProcessor
: Parses the raw (often streaming) output from thellmClient
into structured events like text chunks and tool call requests.toolExecutor: ToolExecutor
: Responsible for taking the tool call requests (parsed byresponseProcessor
) and actually executing the corresponding tools (obtained fromtoolProvider
).contextManager: ContextManager
: Manages the conversation history for the LLM, handling token counting, and preparing the message list to fit within the LLM's context window (potentially including summarization in future versions).runConfig: AgentRunConfig
: The specific configuration settings for this particular run (detailed below).
Why is IAgentContext
important?
Decoupling: Agents (
IAgent
implementations) are not tightly coupled to concrete service implementations. They operate against interfaces.Testability: Makes it easier to mock dependencies when testing agent logic.
Flexibility: Allows different runs of the same agent class to operate in different environments (e.g., with different tools, LLM models, or storage backends) simply by providing a different
IAgentContext
.Dynamic Behavior: Features like dynamic authentication overrides for tools are passed via
runConfig.requestAuthOverrides
within the context.
The ApiInteractionManager
(or the AgentB
facade) is responsible for assembling the correct IAgentContext
before invoking an agent.
AgentRunConfig
: Tailoring Agent Behavior
AgentRunConfig
: Tailoring Agent BehaviorThe AgentRunConfig
object, found within IAgentContext.runConfig
, provides specific settings that fine-tune how an agent behaves during a particular run.
Key Properties of AgentRunConfig
:
model: string
(Mandatory): The identifier of the LLM model to be used (e.g., "gpt-4o-mini", "claude-3-sonnet").systemPrompt?: string
:The overarching instructions given to the LLM to guide its persona, role, and how it should approach tasks.
ApiInteractionManager
often generates a default system prompt based on the operational mode and available tools. This can be overridden here.
temperature?: number
:Controls the randomness of the LLM's output. Higher values (e.g., 0.8) make it more creative/random; lower values (e.g., 0.2) make it more deterministic/focused.
Default is often around 0.7.
maxTokens?: number
:The maximum number of tokens the LLM should generate in its response part of a turn.
toolChoice?: LLMToolChoice
:Instructs the LLM on how to use tools.
'auto'
(default): LLM decides whether to use a tool or respond directly.'none'
: LLM must respond directly, cannot use tools.'required'
: LLM must call at least one tool.{ type: "function", function: { name: "my_specific_tool" } }
: Forces the LLM to call a specific named tool.
maxToolCallContinuations?: number
:The maximum number of times an agent (like
BaseAgent
) will automatically loop back to the LLM after executing tools within a singlerun()
invocation.Helps prevent infinite loops if the LLM repeatedly calls tools.
Default is often around 5 or 10. If exceeded, the run might end in
requires_action
orfailed
.
responseProcessorConfig?: ResponseProcessorConfig
:Configuration for the
LLMResponseProcessor
(e.g.,enableNativeToolCalling
). Seldom needs changing from defaults.
toolExecutorConfig?: ToolExecutorConfig
:Configuration for the
ToolExecutor
(e.g.,executionStrategy: 'sequential' | 'parallel'
for multiple tool calls in one turn).
contextManagerConfig?: ContextManagerConfig
:Settings for the
ContextManager
(e.g.,tokenThreshold
for summarization,summarizationModel
).
requestAuthOverrides?: PerProviderAuthOverrides
:Allows passing dynamic, request-specific authentication details for
OpenAPIConnector
-based tools.Keys are
ToolProviderSourceConfig.id
values, and values areConnectorAuthentication
objects.This is crucial for multi-tenant apps or per-user API keys.
requestContext?: Record<string, any>
:A general-purpose object to pass request-specific contextual information that might be needed by custom tools or deeply within the agent logic.
Other LLM Provider Specific Options:
[key: string]: any;
: TheAgentRunConfig
can also hold additional key-value pairs that are specific to theILLMClient
implementation being used (e.g., custom parameters for an OpenAI call not covered by the standard options).
Configuration Hierarchy:
Library Defaults: AgentB has built-in defaults (e.g., in
DEFAULT_AGENT_RUN_CONFIG
).AgentB.initialize()
Defaults: Options passed toAgentB.initialize({ defaultAgentRunConfig: ... })
.ApiInteractionManagerOptions
Defaults: Options passed tonew ApiInteractionManager({ defaultAgentRunConfig: ... })
.AgentB.getExpressStreamingHttpHandler()
Defaults: Options passed toAgentB.getExpressStreamingHttpHandler({ initialAgentRunConfig: ... })
for a specific HTTP route.Per-Interaction Override: The
agentRunConfigOverride
argument passed directly toaim.runAgentInteraction()
orAgentB.runHttpInteractionStream()
.
Each level can override the previous ones, allowing for flexible configuration from global defaults down to specific requests.
By carefully crafting the IAgentContext
(especially the IToolProvider
) and the AgentRunConfig
, you can precisely control an agent's capabilities, behavior, and interaction style without modifying the agent's core class logic itself.
Last updated