Skip to main content
This example demonstrates how to build an AI agent that can use tools to accomplish tasks.

Overview

Agents are AI systems that can:
  • Reason about which tools to use
  • Call functions with appropriate parameters
  • Maintain conversation context
  • Make decisions based on tool results
This example shows how to create a simple weather agent using the modern @llamaindex/workflow package.

Complete Example

single-agent.ts

Step-by-Step Explanation

1. Define Tools

Tools are functions the agent can call:
Key components:
  • name - Unique identifier for the tool
  • description - Helps the LLM understand when to use the tool
  • parameters - Zod schema defining input parameters
  • execute - The actual function implementation

2. Create the Agent

3. Run the Agent

4. Maintain State

Reuse conversation context for follow-up questions:

More Tool Examples

User Information Tool

Random ID Generator

Query Engine as a Tool

Use a RAG query engine as a tool:

Using Different LLM Providers

Anthropic (Claude)

Ollama (Local Models)

Running the Example

  1. Install dependencies:
  1. Set your API key:
  1. Run the example:

Expected Output

The agent will:
  1. Receive your question
  2. Decide to use the get_weather tool
  3. Call the tool with extracted parameters
  4. Use the tool result to formulate a response

Advanced Features

Multiple Agents

Coordinate multiple specialized agents:

Memory and Context

Add memory to your agents:

Streaming Responses

Stream agent responses in real-time:

Next Steps

Workflows

Build complex multi-step workflows

Agent Memory

Add memory and context to agents

Custom Tools

Create sophisticated custom tools

Multi-Agent

Coordinate multiple agents