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:

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

Agent Construction Workflow

  1. Instantiate Config: The developer creates a Config object populating fields to specify the agent’s name, description, model, tools, instructions, callbacks, and other parameters.

  2. Create Agent via New(cfg Config): This function converts the configuration into a fully operational llmAgent instance. It wraps the configured callbacks into internal types, sets up the agent’s state, and creates a base agent with lifecycle callbacks.

  3. 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 OutputKey is set.

  4. 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

Output Handling

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:

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