Daydreams Logo

Your first agent

Build your first Daydreams agent.

Overview

Daydreams is a framework for building autonomous AI agents. At its core, an agent operates through a continuous cycle:

  1. Analyzes incoming information (inputs)
  2. Reasons about it using a Large Language Model (LLM)
  3. Decides on the next steps - either generating a response (output) or performing a task (action)
  4. Feeds results back into the agent's awareness, creating a continuous loop orchestrated by the LLM

This enables you to build agents that can interact with various systems like blockchains, social media platforms, APIs, and more, all based on predefined goals and contextual understanding.

Installation

Install the core Daydreams packages:

pnpm add @daydreamsai/core @daydreamsai/cli

You'll also need an LLM provider SDK. For this guide, we'll use OpenAI:

pnpm add @ai-sdk/openai

Important: Make sure you have an OPENAI_API_KEY environment variable set before proceeding.

Core Concepts

Daydreams is built around several key components that work together:

Essential Components

  • Agent Lifecycle - The central orchestrator that runs the main loop
  • Contexts - Manages state and memory for specific tasks or interactions (e.g., a chat session)
  • Inputs - How agents receive information (e.g., CLI messages, API events)
  • Outputs - How agents respond or send information (e.g., CLI responses, tweets)
  • Actions - Tasks agents can perform (e.g., calling APIs, executing transactions)
  • Memory - How agents store and recall information (working memory, episodic memory)

For detailed information about these concepts, visit the Core Concepts section.

Your First Agent (CLI Echo Bot)

Let's build a simple agent that echoes back whatever you type in the command line. This example demonstrates the basic structure and workflow of a Daydreams agent.

Step 1: Set up your project

create-project.sh
mkdir my-first-agent && cd my-first-agent

pnpm add @daydreamsai/core @daydreamsai/cli @ai-sdk/openai zod

Step 2: Create your agent

Create a file named agent.ts:

agent.ts
import { createDreams, context, input, output } from "@daydreamsai/core";
import { cliExtension } from "@daydreamsai/cli";
import { openai } from "@ai-sdk/openai";
import { z } from "zod";
 
// 1. Define the main context for our agent
const echoContext = context({
  type: "echo",
  // No specific arguments needed for this simple context
  schema: z.object({}),
  // Instructions that guide the LLM's behavior
  instructions:
    "You are a simple echo bot. Repeat the user's message back to them.",
});
 
// 2. Create the agent instance
const agent = createDreams({
  // Configure the LLM model to use
  model: openai("gpt-4o-mini"),
  // Include the CLI extension for input/output handling
  extensions: [cliExtension],
  // Register our custom context
  contexts: [echoContext],
});
 
// 3. Start the agent and run the context
async function main() {
  // Initialize the agent (sets up services like readline)
  await agent.start();
 
  console.log("Echo agent started. Type 'exit' to quit.");
 
  // Run our echo context
  // The cliExtension automatically handles console input/output
  await agent.run({
    context: echoContext,
    args: {}, // Empty object since our schema requires no arguments
  });
 
  // Agent stops when the input loop breaks (e.g., user types "exit")
  console.log("Agent stopped.");
}
 
// Start the application
main();

Step 3: Run your agent

Ensure your OPENAI_API_KEY environment variable is set, then run:

run-agent.sh
node agent.ts

Your agent will start listening for input. Type any message and watch as the agent echoes it back using the LLM and CLI handlers provided by the cliExtension.


Next Steps

Continue learning about Daydreams with these resources:

On this page