Building AI Agents with ADK Go

By Google for Developers

Share:

Key Concepts

  • Go ADK (Agent Development Kit): A handcrafted AI agent library idiomatic for Go, designed to simplify the development, deployment, and evaluation of AI agents.
  • AI Agents: Programs that can reason, interact with tools, and make decisions, often powered by Large Language Models (LLMs).
  • LLM-based Reasoning Agents: Agents that decide which tools to call or which other agents to transfer tasks to, based on LLM analysis.
  • Workflow Agents: Agents designed for prescriptive and deterministic logic flows, orchestrating sequences or parallel execution of other agents.
  • Custom Agents: Agents that allow for arbitrary logic, combining LLM capabilities with workflow or other custom code.
  • Tools: External capabilities that augment LLMs, allowing agents to browse the internet, query databases, send emails, or call APIs.
  • Function Tools: A type of tool that wraps arbitrary Go code, making it accessible to the agent.
  • Session: Represents a single ongoing conversation thread with a user, storing history, shared state, and metadata.
  • State: An agent's temporary scratchpad within a session, used to store conversation-specific data.
  • Events: A sequence of interactions, including messages, tool calls, and results, stored within a session.
  • Multi-agent Architectures: Systems where multiple agents collaborate, either by using each other as tools or through workflow orchestration.
  • Agent-to-Agent Protocol: A mechanism for agents to communicate and interoperate.
  • Sequential Agent: A workflow agent that executes a series of agents one after another, passing outputs between them.
  • Parallel Agent: A workflow agent that executes sub-agents concurrently.
  • Loop Agent: A workflow agent that repeatedly executes a chain of agents until a specified exit condition is met.
  • Memory Service: A component for managing long-term knowledge that persists beyond a single conversation.

Introduction to Go ADK

Ivan, an engineer on the agent development kit team, introduces Go ADK, a new AI agent library specifically designed for the Go programming language. The primary goal of ADK is to address the complexity of building AI agents from scratch, which often involves managing state, memory, tool calls, and control flow, leading to repetitive development. ADK aims to provide a flexible, modular, and open-source framework to streamline agent development, deployment, and evaluation.

Getting Started with Go ADK

The initial step to using Go ADK involves adding it as a dependency to a Go project using the go get command.

Categories of Agents in ADK

ADK categorizes agents into three main types:

  1. LLM-based Reasoning Agents: These agents leverage LLMs to analyze user requests and decide on the next steps, such as calling specific tools or transferring the task to another agent.
  2. Workflow Agents: These agents are designed for more structured and deterministic logic flows. They orchestrate sequences of actions or agent interactions.
  3. Custom Agents: This category allows developers to write arbitrary logic, enabling the combination of LLM-based reasoning with workflow agents or other custom code.

Running Your First Go Agent

A basic agent, such as a science teacher assistant, can be defined with a name, description, an LLM model object, and an instruction prompt. To run the agent, an in-memory session service is used to create a session for the conversation and a runner to execute the agent. The agent's output is an iterator of events that can be printed. This can be run like any other Go program using go run. The example demonstrates a simple interaction where the agent responds to "hello" and then answers a science question.

A web UI interface is also available, allowing users to select their agent and interact with it. The "event tab" in the UI visualizes the flow of events during the agent's execution.

Augmenting Agents with Tools

To enable agents to handle information beyond their training data, such as recent events, they can be equipped with tools. LLMs, while powerful with text and multimedia, cannot natively browse the internet, query databases, send emails, or call external APIs. Tools provide these augmented capabilities.

The process of tool usage involves:

  1. The agent receives a user request.
  2. The LLM analyzes the request and determines if an external action is needed.
  3. The LLM identifies the appropriate registered tool and its arguments.
  4. The ADK framework intercepts this function call.
  5. The framework executes the code defined for the tool.
  6. The tool's result is returned to the agent, often back to the LLM, for processing and formulating the final user response.

ADK abstracts this entire execution loop, offering a clean interface for defining and managing tools.

Types of Tools in Go ADK

Go ADK supports three types of tools:

  1. Built-in Tools: Examples include Google Search.
  2. Third-Party Tools: Tools provided by external services, such as MCP servers.
  3. Function Tools: These tools wrap arbitrary Go code, making it accessible to the agent.

Example: Function Tool for Order Status

For a customer service agent, a function tool can be added to check order status. This involves:

  • Defining structs for input and return values.
  • Writing a Go function containing the custom logic (e.g., returning the status for all orders).
  • Using the functionTool function to convert this Go function into a tool object.
  • Providing a detailed description for the LLM to understand when to invoke the tool.

Prompt Quality and Agent Definition

The quality of prompts is crucial for defining agent behavior. Precise and explicit instructions are needed to define the agent's purpose, persona, goals, and interaction guidelines. Tools must also be described in detail so the LLM can accurately understand their purpose and arguments.

With these steps—adding dependencies, defining tools, configuring the agent, and using the runner—a functional agent capable of understanding natural language and performing actions through defined tools can be quickly developed.

Implementing More Complex Concepts: Multi-Agent Architectures

Beyond single agents, Go ADK supports multi-agent collaboration. This is achieved through:

  • Interoperability for Tools via MCP: (Mentioned as a topic for future videos).
  • Interoperability for Agents via Agent-to-Agent Protocol: (Mentioned as a topic for future videos).
  • Native Functionality for Multi-Agent Architectures:

Agent as a Tool

A powerful pattern is allowing one agent to use another agent as a tool. This "agent tool" concept enables hierarchical or collaborative systems. For instance, a main agent needing to summarize long text can use a dedicated "summarizer agent" wrapped as a tool.

Workflow Agents for Orchestration

ADK's workflow agents orchestrate complex flows involving multiple agents:

  • Sequential Agent: Executes a series of agents in order, passing outputs from one to the next.
  • Parallel Agent: Executes sub-agents concurrently. This is useful for tasks like research where multiple agents can gather information simultaneously, with results then aggregated.
  • Loop Agent: Executes a chain of agents sequentially, potentially multiple times, until an exit condition is met. An example is an iterative story writing system that refines a draft until a critique agent is satisfied.

Deciding on Agent Architecture

The choice between a single agent, multiple agents, or a workflow depends on the problem:

  • Single Agent: Suitable for tasks requiring reasoning and dynamic tool use without easily decomposable sub-problems, or when the number of tools is manageable for the LLM. The science teacher agent is an example.
  • Multiple Agents: Ideal for complex problems that naturally break down into smaller subtasks or require distinct expertise. An e-commerce order processing system is cited, with agents for inventory, payment, and shipping. This offers modularity but may incur communication overhead.
  • Workflow Agents: Used to orchestrate complex, multi-step processes, especially when sequential or parallel execution is beneficial.

The decision hinges on problem scope, required flexibility, and the need for distinct areas of intelligence.

Context and Knowledge Management: Sessions and State

Effective context and knowledge management are crucial for ongoing interactions. ADK provides tools for this through the session concept.

  • Session: Represents a single conversation thread, identified by a unique ID, application name, and user ID. It stores:

    • History: A record of all interactions, messages, tool calls, and results as a sequence of events.
    • Shared State Map: A key-value store for conversation-specific data.
    • Last Event Timestamp: Indicates when the last interaction occurred.
  • State: The agent's temporary scratchpad within a session. It can be initialized with specific values. The science teacher example is revisited, showing how instructions can use state variables (e.g., topic, audience) defined during session initialization. State can be updated via callbacks, tools, or a special output key mechanism.

For knowledge that needs to persist beyond a single conversation, ADK offers a memory service, which will be covered in future videos.

Conclusion

The introduction to Go ADK covers key concepts like sessions, state, and events. It demonstrates how agents can use tools and even call other agents. By combining workflow orchestration, multi-agent collaboration, and robust state and memory management, ADK provides the building blocks for creating sophisticated AI agents. Developers are encouraged to start building and explore the ADK documentation and samples available in Python, Java, and Go.

Chat with this Video

AI-Powered

Load the transcript when you're ready to chat so the initial page stays lighter.

Ready to summarize another video?

Summarize YouTube Video