Class Agent

java.lang.Object
com.google.genkit.ai.Agent

public class Agent extends Object
Represents an agent that can be used as a tool in multi-agent systems.

An Agent wraps an AgentConfig and provides a Tool interface for delegation. When the model calls an agent as a tool, the agent's configuration (system prompt, model, tools) is applied to the conversation, effectively "transferring" control to the specialized agent.

Example usage:


 // Define a specialized agent
 Agent reservationAgent = genkit
 		.defineAgent(AgentConfig.builder().name("reservationAgent").description("Handles restaurant reservations")
 				.system("You are a reservation specialist...").tools(List.of(reservationTool)).build());

 // Use in a parent agent
 Agent triageAgent = genkit.defineAgent(AgentConfig.builder().name("triageAgent").description("Routes requests")
 		.system("Route customer requests to specialists").agents(List.of(reservationAgent.getConfig())).build());

 // Start chat with triage agent
 Chat chat = genkit.chat(triageAgent);
 
  • Constructor Details

    • Agent

      public Agent(AgentConfig config)
      Creates a new Agent.
      Parameters:
      config - the agent configuration
  • Method Details

    • getConfig

      public AgentConfig getConfig()
      Gets the agent configuration.
      Returns:
      the config
    • getName

      public String getName()
      Gets the agent name.
      Returns:
      the name
    • getDescription

      public String getDescription()
      Gets the agent description.
      Returns:
      the description
    • getSystem

      public String getSystem()
      Gets the system prompt.
      Returns:
      the system prompt
    • getModel

      public String getModel()
      Gets the model name.
      Returns:
      the model name
    • getTools

      public List<Tool<?,?>> getTools()
      Gets the tools available to this agent.
      Returns:
      the tools
    • getAgents

      public List<AgentConfig> getAgents()
      Gets the sub-agents.
      Returns:
      the sub-agents
    • getAllTools

      public List<Tool<?,?>> getAllTools(Map<String,Agent> agentRegistry)
      Gets all tools including sub-agent tools for handoff pattern.

      This method collects all tools that should be available to the agent, including the agent's own tools and sub-agents as tools (for handoff). When a sub-agent tool is called, the Chat will handle the handoff by switching context to that agent.

      Parameters:
      agentRegistry - map of agent name to Agent instance
      Returns:
      combined list of all tools from this agent and sub-agents as tools
    • asTool

      Returns this agent as a tool that can be used by other agents.
      Returns:
      the agent as a tool
    • getToolDefinition

      public ToolDefinition getToolDefinition()
      Gets the tool definition for this agent.
      Returns:
      the tool definition
    • toString

      public String toString()
      Overrides:
      toString in class Object