Agent Development Kit (ADK) for Go

repository·main·Indexed 27 days ago

https://github.com/google/adk-go

A modular, code-first framework for building, evaluating, and deploying AI agents. Designed for high-performance, cloud-native applications, adk-go is model-agnostic and concurrency-optimized. It includes the adkgo CLI for deployment and testing, and provides various launcher implementations for environments including AgentEngine, Cloud Run, REST APIs, and interactive consoles.

Tokens
5K
Snippets
10
Records
36
Agent score
94%

What's inside adk-go

  1. Overview of Agent Development Kit (ADK) for Go

    main

    Agent Development Kit (ADK) is an open-source, code-first Go toolkit designed for building, evaluating, and deploying sophisticated AI agents. It applies software development principles to agent creation, allowing for modular multi-agent systems and flexible orchestration.

    Key characteristics include:

    • Model-agnostic: While optimized for Gemini, it works with other models.
    • Deployment-agnostic: Supports containerization and cloud-native environments like Google Cloud Run.
    • Code-first: Agent logic, tools, and orchestration are defined directly in Go for better testability and versioning.
    • Concurrency-optimized: Leverages Go's strengths for high-performance agent applications.
  2. Migrate `session.NewEvent` to v2.0

    main

    In ADK v2.0, session.NewEvent requires a context.Context as its first argument. This change allows the platform package to use providers (via platform.WithTimeProvider and platform.WithUUIDProvider) installed on the context to manage event IDs and timestamps, enabling deterministic and replay-safe event production.

    Migration Steps:

    • Replace the old parameterless-context form or NewEventWithContext with the new signature.
    • Pass the existing context from the scope (e.g., the context from an agent, tool, callback, or incoming RPC/HTTP request).
    • Avoid using context.Background() in the middle of call chains; thread the existing context through your helpers.
    // Before
    ev := session.NewEvent(ctx.InvocationID())
    // or
    ev := session.NewEventWithContext(ctx, ctx.InvocationID())
    
    // After
    ev := session.NewEvent(ctx, ctx.InvocationID())
  3. Update Mocks for Unified Contexts

    main

    ADK v2.0 merges ToolContext and CallbackContext into a single agent.Context. If you are using custom mock contexts, they will likely break because they are missing new methods related to the unified surface.

    You have two options to fix this:

    1. Manual Update: Manually add the missing methods to your mock implementation.
    2. Embed agent.StrictContextMock (Recommended): Embed agent.StrictContextMock in your test fake. This ensures your mock remains compatible even as the agent.Context interface grows. Un-overridden methods in StrictContextMock will panic with "not implemented", ensuring unexpected calls fail tests loudly.
    // Recommended: Embed StrictContextMock and override only what the test needs.
    type fakeContext struct {
    	agent.StrictContextMock
    }
    
    var _ agent.Context = (*fakeContext)(nil)
    
    func TestSomething(t *testing.T) {
    	cc := &fakeContext{agent.StrictContextMock{Ctx: context.Background()}}
    	// Override methods as needed, e.g. by adding them on fakeContext.
    	// ...
    }
  4. Use the agentengine sublauncher

    main

    The agentengine sublauncher starts an AgentEngine server that serves the reasoning engine API. This is used when deploying to Agent Engine.

    Local Access URLs:

    • [webUrl]/api/reasoning_engine
    • [webUrl]/api/stream_reasoning_engine

    Deployed Access URLs (Google Cloud):

    • https://${LOCATION_ID}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${LOCATION_ID}/reasoningEngines/${RESOURCE_ID}:query
    • https://${LOCATION_ID}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${LOCATION_ID}/reasoningEngines/${RESOURCE_ID}:streamQuery
  5. Use the Web Launcher to run ADK

    main

    The web launcher allows you to run the Agent Development Kit (ADK) using a web server. It is designed to be extended via Sublauncher implementations, which add specific routes and functionality to the server.

    To use the web launcher, you must provide at least one Sublauncher. The command-line syntax follows a pattern where you specify the web command followed by its flags, and then specify the keywords for the sublaunchers you wish to activate.

    Web Launcher Flags:

    • -port: Localhost port for the server (default: 8080)
    • -write-timeout: Server write timeout (e.g., '10s', '2m') (default: 15s)
    • -read-timeout: Server read timeout (e.g., '10s', '2m') (default: 15s)
    • -idle-timeout: Server idle timeout (e.g., '10s', '2m') (default: 60s)
    • -otel_to_cloud: Enables/disables OpenTelemetry export to GCP (default: false)
  6. Use the Universal Launcher to route CLI arguments

    main

    The universal package provides a launcher.Launcher implementation that acts as a router for multiple sublaunchers (such as console or web). It selects a sublauncher based on the first command-line argument (the keyword).

    Routing Logic:

    • If the first argument matches a sublauncher's Keyword(), that sublauncher is used to parse the remaining arguments.
    • If no arguments are provided, or the first argument does not match any known keyword, the first sublauncher in the list provided to NewLauncher is used as the default.
    • Sublaunchers must have unique keywords; otherwise, NewLauncher will fail during the parsing phase.
  7. Implement the SubLauncher interface for composed modes

    main

    If you are building a parent launcher (like a universal launcher) that supports multiple modes of operation (e.g., 'console' or 'web'), implement the SubLauncher interface. Each SubLauncher is activated by a specific command-line keyword.

    type SubLauncher interface {
    	// Keyword returns the command-line keyword that activates this sub-launcher.
    	Keyword() string
    	// Parse parses the arguments for the sub-launcher. It should return any unparsed arguments.
    	Parse(args []string) ([]string, error)
    	// CommandLineSyntax returns a string describing the command-line flags and arguments for the sub-launcher.
    	CommandLineSyntax() string
    	// SimpleDescription provides a brief, one-line description of the sub-launcher's function.
    	SimpleDescription() string
    	// Run executes the sub-launcher's main logic.
    	Run(ctx context.Context, config *Config) error
    }
  8. Implement the Launcher interface

    main

    To create a main entrypoint for an ADK application, implement the Launcher interface. This interface is responsible for parsing command-line arguments and executing the application logic. You must provide implementations for Execute and CommandLineSyntax.

    type Launcher interface {
    	// Execute parses command-line arguments and runs the launcher.
    	Execute(ctx context.Context, config *Config, args []string) error
    	// CommandLineSyntax returns a string describing the command-line flags and arguments.
    	CommandLineSyntax() string
    }