LLM Agent Configuration
Purpose
The LLM Agent Configuration subtopic focuses on defining and managing the configuration details essential for creating and operating Large Language Model (LLM) agents within the broader LLM Integration and Agents topic. While the parent topic describes specialized agents interacting with LLMs and their composable components, this subtopic addresses the concrete specification of those agents' behavior through configurable parameters. It encapsulates settings such as the LLM model to use, the set of callable tools, instructions guiding the agent's responses, and lifecycle callbacks that customize execution.
This configuration layer is crucial because LLM agents must be highly adaptable to different use cases, contexts, and interaction patterns. The configuration enables:
Specifying which LLM model powers the agent (e.g., Google Gemini).
Defining the tools and toolsets accessible by the agent.
Providing pre- and post-processing callbacks for model and tool invocations.
Customizing instructions or prompt templates to shape agent behavior dynamically.
Controlling agent lifecycle behavior including sub-agent delegation and state management.
Managing input/output validation schemas to ensure structured communication.
Without this explicit configuration abstraction, agents would either be rigid or require ad-hoc coding changes for each new behavior or integration scenario.
Functionality
The core functionality of the LLM Agent Configuration revolves around the Config struct, which aggregates all parameters required to instantiate and run an LLM agent. The configuration controls every major aspect of the agent’s lifecycle and interaction with the LLM and tools:
Key Configuration Elements
Model Specification (
Model): Defines the underlying LLM instance that the agent will query. This could be any model implementing themodel.LLMinterface, enabling pluggable backends.Tools and Toolsets (
Tools,Toolsets): Lists the tools directly callable by the agent and toolsets that provide collections of tools, facilitating modular extension of agent capabilities.Instruction Handling (
Instruction,InstructionProvider,GlobalInstruction,GlobalInstructionProvider): Instructions guide the LLM prompt construction. These can be static templates with placeholders or dynamically generated via providers based on runtime context. The global instruction applies to all agents in the agent hierarchy, enabling consistent agent personality or identity.Callbacks (
BeforeModelCallbacks,AfterModelCallbacks,BeforeToolCallbacks,AfterToolCallbacks): Lifecycle hooks allow interception and modification of requests and responses at both model and tool invocation stages. This supports logging, caching, metrics, or custom logic injection.Agent Composition (
SubAgents): Defines child agents to which tasks can be delegated. The configuration ensures proper parent-child relationships enabling agent transfer and escalation.Session State Management (
OutputKey,InputSchema,OutputSchema): Controls how outputs are saved into session state and validates input/output data formats to maintain structured communication.Behavior Controls (
DisallowTransferToParent,DisallowTransferToPeers,IncludeContents): Flags to restrict agent transfer behavior and to control the inclusion of conversation history in model requests.
Agent Construction Workflow
Instantiate Config: The developer creates a
Configobject populating fields to specify the agent’s name, description, model, tools, instructions, callbacks, and other parameters.Create Agent via
New(cfg Config): This function converts the configuration into a fully operationalllmAgentinstance. It wraps the configured callbacks into internal types, sets up the agent’s state, and creates a base agent with lifecycle callbacks.Run Agent Execution: On invocation, the agent uses the configured model and tools, applies instructions (static or dynamic), and executes the callbacks in the order specified. The agent manages output saving to session state if the
OutputKeyis set.Lifecycle Callbacks: Before and after model calls and tool runs, the configured callbacks can modify requests, short-circuit calls (e.g., caching), log responses, or handle errors.
Instruction Handling Specifics
The
Instructionfield supports templated strings with placeholders referencing session state variables or artifacts, allowing runtime injection of context data.The
InstructionProvidercallback offers dynamic instruction generation based on the current invocation context and replaces the static instruction if provided.The
GlobalInstructionand its provider apply at the root agent level to enforce consistent instructions across the agent tree.
Output Handling
The agent optionally saves the final model output into the session state under the key specified by
OutputKey, enabling further downstream use by other agents or tools.Output saving respects partial response flags and skips saving if the event is authored by another agent.
Example Code Snippet
The following snippet shows how callbacks are wrapped and the agent is constructed from the configuration:
beforeModelCallbacks := make([]llminternal.BeforeModelCallback, 0, len(cfg.BeforeModelCallbacks))
for _, c := range cfg.BeforeModelCallbacks {
beforeModelCallbacks = append(beforeModelCallbacks, llminternal.BeforeModelCallback(c))
}
a := &llmAgent{
beforeModelCallbacks: beforeModelCallbacks,
model: cfg.Model,
instruction: cfg.Instruction,
// other fields...
}
baseAgent, err := agent.New(agent.Config{
Name: cfg.Name,
Description: cfg.Description,
SubAgents: cfg.SubAgents,
BeforeAgentCallbacks: cfg.BeforeAgentCallbacks,
Run: a.run,
AfterAgentCallbacks: cfg.AfterAgentCallbacks,
})
This illustrates the essential step of translating configuration into an executable agent instance.
Integration
The LLM Agent Configuration tightly integrates with multiple components within the LLM Integration and Agents topic and beyond:
Parent Topic (
LLM Integration and Agents): This configuration directly defines the parameters that shape the specialized LLM agents described at a higher level, enabling them to operate with specific models, tools, and behaviors.Tools (
Tooling System80556): Tools and toolsets specified in the configuration determine which capabilities the agent can invoke, connecting to the modular tool framework described elsewhere.Instruction Template Processing (
Instruction Template Processing80563): The instruction templates or providers configured here rely on runtime injection and processing of session state and artifact data as handled by instruction injection subsystems.Agent Framework (
AI Agent Framework80561): The base agent constructed here utilizes core lifecycle and invocation interfaces from the agent framework, ensuring consistent execution patterns.Session Management (
Session Management80559): Output keys and state deltas configured in the agent allow integration with session persistence, enabling multi-turn conversations and stateful interactions.Telemetry (
Telemetry and Observability80566): Callbacks configured here provide extension points for instrumentation and tracing of LLM and tool calls, feeding into the observability infrastructure.Agent Workflow Management (80558): The
SubAgentsconfigured in the hierarchy enable complex workflows with agent transfer and delegation as orchestrated by workflow agents.
By defining this configuration abstraction, the system ensures that customizable agent instances can be created without modifying code, thus supporting scalability and flexibility.
Diagram
classDiagram
class Config {
+string Name
+string Description
+[]Agent SubAgents
+[]BeforeAgentCallback BeforeAgentCallbacks
+[]AfterAgentCallback AfterAgentCallbacks
+GenerateContentConfig GenerateContentConfig
+[]BeforeModelCallback BeforeModelCallbacks
+model.LLM Model
+[]AfterModelCallback AfterModelCallbacks
+string Instruction
+InstructionProvider InstructionProvider
+string GlobalInstruction
+InstructionProvider GlobalInstructionProvider
+bool DisallowTransferToParent
+bool DisallowTransferToPeers
+IncludeContents IncludeContents
+*genai.Schema InputSchema
+*genai.Schema OutputSchema
+[]BeforeToolCallback BeforeToolCallbacks
+[]tool.Tool Tools
+[]AfterToolCallback AfterToolCallbacks
+[]tool.Toolset Toolsets
+string OutputKey
}
class llmAgent {
+Agent Agent
+State llminternal.State
+[]BeforeModelCallback beforeModelCallbacks
+model.LLM model
+[]AfterModelCallback afterModelCallbacks
+string instruction
+[]BeforeToolCallback beforeToolCallbacks
+[]AfterToolCallback afterToolCallbacks
+*genai.Schema inputSchema
+*genai.Schema outputSchema
+run()
+maybeSaveOutputToState()
}
Config --> llmAgent : Instantiates
llmAgent --> model.LLM : Uses
llmAgent --> tool.Tool : Integrates Tools
llmAgent --> agent.Agent : Embeds Base Agent
llmAgent --> llminternal.State : Maintains State
llmAgent --> BeforeModelCallback : Executes
llmAgent --> AfterModelCallback : Executes
llmAgent --> BeforeToolCallback : Executes
llmAgent --> AfterToolCallback : Executes