Build your first Typescript agent with Google ADK

By Google for Developers

AI TechnologyAgent DevelopmentSoftware EngineeringTypeScript
Share:

TypeScript Agent Development Kit (ADK) – A Deep Dive

Key Concepts:

  • Agent Development Kit (ADK): A framework for simplifying AI agent development and deployment.
  • LLM-based Agents: Agents utilizing Large Language Models (LLMs) for reasoning and decision-making.
  • Workflow Agents: Agents designed for deterministic logic flows and orchestration.
  • Custom Agents: Agents allowing developers to implement custom logic and behavior.
  • Tools: Mechanisms for agents to interact with the external world (e.g., APIs, databases).
  • Function Tools: Tools wrapping custom code for agent use.
  • Agent-to-Agent (A2A): A pattern enabling agents to utilize other agents as tools.
  • Workflow Agents (Sequential, Parallel, Loop): Types of agents for orchestrating complex multi-agent flows.
  • Session: Manages multiple conversations, storing history and state.
  • State: Temporary storage within a session for conversation-specific data.
  • Memory: (Mentioned but not detailed in this video - likely covered in other resources) Long-term storage of information.
  • Function Calling: LLM capability to determine when to use external tools.

1. Introduction to TypeScript ADK

Mandy introduces the TypeScript Agent Development Kit (ADK), an open-source framework designed to streamline the creation and deployment of AI agents using TypeScript and JavaScript. She highlights the complexity of building agents from scratch, involving managing state, memory, tool calls, and control flow, often leading to redundant efforts. ADK aims to address these challenges by providing a flexible and modular solution. Links to introductory videos and workshops are provided in the video description.

2. Setting Up Your Project & Core Agent Concepts

The initial step involves adding the ADK dependency to your project’s package.json file using npm or yarn. An optional dependency, @ashdev/tools, can be added for the Development UI (Dev UI), a visual interface for prototyping and debugging agents.

The fundamental building block within ADK is the agent. Three main agent categories are defined:

  • LLM-based Agents: These agents leverage LLMs for reasoning, deciding which tools to invoke, or which other agents to delegate tasks to.
  • Workflow Agents: These are suited for scenarios requiring predictable and deterministic logic flows.
  • Custom Agents: These provide maximum flexibility, allowing developers to implement their own logic from scratch.

These agent types can be combined to create more sophisticated systems.

3. Running Your First TypeScript Agent

A simple example demonstrates creating an LLM-based agent acting as a "helpful assistant" for answering student questions about scientific concepts. The agent is instantiated with a configuration object specifying its name, description, the LLM model to use, and instructions defining its role.

To execute the agent, a runner and a session are required. The runner coordinates the application, while the session maintains the conversation state. A while loop is used to continuously take user input, pass it to the agent, and display the agent’s response, which is returned as a list of events. The code can be run directly from the TypeScript source file using ts-node.

A live demonstration shows the agent responding to questions like "What about quantum computing?" and "Why is the sky blue?". The Dev UI is also showcased, allowing interaction with the agent through a web console and visualizing the events flowing through the system, including tool calls and responses.

4. Integrating Tools for External Interaction

LLMs, while powerful with text, lack native capabilities for tasks like web browsing or database queries. Tools bridge this gap, enabling agents to interact with the external world. This functionality is supported by the LLM’s function calling capability.

The process involves:

  1. The agent receives a user request.
  2. The LLM analyzes the request and determines if a tool is needed.
  3. The LLM selects the appropriate tool and its arguments.
  4. ADK intercepts the function call, executes the tool’s code, and retrieves the result.
  5. The tool’s result is sent back to the agent (typically to the LLM) for processing and response generation.

ADK provides three types of tools:

  • Function Tools: Wrap custom code.
  • Built-in Tools: Like Google Search.
  • Third-Party Tools: Provided by services like MCP.

5. Implementing Function Tools

A customer service example demonstrates creating a function tool to retrieve order status information. This involves:

  1. Defining a TypeScript method that represents the external function.
  2. Creating an instance of FunctionTool, passing a configuration object that defines the tool’s name, description, and parameters using a JSON schema. The schema helps the LLM understand when to call the function and what arguments it expects.
  3. The function returns a structured response (serialized as JSON) to the LLM.

The Dev UI demonstrates the tool in action, showing the events associated with a user query about order status, including the tool call and the returned result.

6. Multi-Agent Systems & Workflow Orchestration

ADK supports building multi-agent systems through extensibility with MCP and A2A protocols (details to be covered in future videos). A key pattern is using one agent as a tool for another (Agent-to-Agent).

Workflow Agents are crucial for orchestrating complex multi-agent flows. Three types are available:

  • Sequential Agents: Chain agents together, passing the output of one as input to the next.
  • Parallel Agents: Execute agents concurrently, combining their outputs.
  • Loop Agents: Run a sequence of agents iteratively until a termination condition is met.

Workflow agents are particularly useful when the logic can be represented as a flowchart, making the execution flow more deterministic. A story-writing system using a loop agent and a critique agent is given as an example.

7. Context and Knowledge Management: Sessions, State, and Memory

Effective handling of ongoing interactions requires robust context and knowledge management. ADK provides tools for this through Sessions, State, and Memory.

  • Sessions: Manage multiple conversations, storing conversation history, a shared state map, and timestamps.
  • State: A temporary scratchpad within a session for storing conversation-specific data. Values stored in the state are serializable (basic types or simple collections). Prefixes can be used to control the state’s lifecycle.
  • Memory: (Not detailed in this video) Likely refers to long-term storage of information.

The example demonstrates specifying the initial state of the science teacher agent, including the subject and audience, which can then be accessed within the agent’s instructions using templating syntax.


Conclusion:

The TypeScript ADK provides a powerful and flexible framework for building sophisticated AI agents. By combining workflow orchestration, multi-agent collaboration, and robust state management, developers can tackle complex problems and create intelligent applications. The video encourages viewers to explore the documentation, sample examples, and future videos to further their understanding and begin building with the TypeScript ADK.

Chat with this Video

AI-Powered

Hi! I can answer questions about this video "Build your first Typescript agent with Google ADK". What would you like to know?

Chat is based on the transcript of this video and may not be 100% accurate.

Related Videos

Ready to summarize another video?

Summarize YouTube Video