---
title: "Chat Examples"
description: "Interactive CLI chat with elizaOS (TypeScript)"
---

The chat example is the simplest way to explore the runtime: create an agent, send messages, and stream replies in the terminal.

## Location

| Language   | File                      | Runtime   |
| ---------- | ------------------------- | --------- |
| TypeScript | `examples/chat/chat.ts`   | Bun / Node |

---

## Quick Start

```bash
# From the repository root
export OPENAI_API_KEY="your-key"
bun run examples/chat/chat.ts
```

---

## Complete source (reference)

```typescript
import {
  AgentRuntime,
  ChannelType,
  createMessageMemory,
  stringToUuid,
  type Character,
  type UUID,
} from "@elizaos/core";
import { openaiPlugin } from "@elizaos/plugin-openai";
import { plugin as sqlPlugin } from "@elizaos/plugin-sql";
import { v4 as uuidv4 } from "uuid";
import * as readline from "readline";

const character: Character = {
  name: "Eliza",
  bio: "A helpful AI assistant.",
};

console.log("🚀 Starting Eliza...\n");

const runtime = new AgentRuntime({
  character,
  plugins: [sqlPlugin, openaiPlugin],
});
await runtime.initialize();

const userId = uuidv4() as UUID;
const roomId = stringToUuid("chat-room");
const worldId = stringToUuid("chat-world");

await runtime.ensureConnection({
  entityId: userId,
  roomId,
  worldId,
  userName: "User",
  source: "cli",
  channelId: "chat",
  serverId: "server",
  type: ChannelType.DM,
});

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

console.log("💬 Chat with Eliza (type 'exit' to quit)\n");

const prompt = () => {
  rl.question("You: ", async (input) => {
    const text = input.trim();

    if (text.toLowerCase() === "exit") {
      console.log("\n👋 Goodbye!");
      rl.close();
      await runtime.stop();
      process.exit(0);
    }

    if (!text) {
      prompt();
      return;
    }

    const message = createMessageMemory({
      id: uuidv4() as UUID,
      entityId: userId,
      roomId,
      content: { text },
    });

    process.stdout.write("Eliza: ");
    await runtime.messageService!.handleMessage(
      runtime,
      message,
      async (content) => {
        if (content?.text) {
          process.stdout.write(content.text);
        }
        return [];
      },
    );

    console.log("\n");
    prompt();
  });
};

prompt();
```

---

## Key concepts

**Character** — personality and optional system text for the agent.

**Runtime** — `AgentRuntime` with plugins (here `plugin-sql` + `plugin-openai`), then `initialize()`.

**Messages** — build a memory with `createMessageMemory`, call `messageService.handleMessage` with an optional streaming callback.

---

## Next Steps

<CardGroup cols={2}>
  <Card title="REST API Examples" icon="server" href="/examples/rest-api">
    Expose your agent over HTTP
  </Card>
  <Card title="Browser Examples" icon="browser" href="/examples/browser">
    Run agents in the browser
  </Card>
</CardGroup>
