0
Fork 0
mirror of https://github.com/mastra-ai/mastra.git synced 2026-11-17 08:44:46 +00:00
mastra/examples/durable-agents
History 2026-11-10 20:07:34 -08:00
..
src/mastra chore(docs): AX improvements (#19631) 2026-07-17 14:29:48 +02:00
docker-compose.yaml chore(deps): update docker images (#28011) 2026-10-29 15:53:50 +00:00
package.json chore(deps): force minimatch 11 and glob 14 for Vanta findings (#29916) 2026-11-10 12:57:20 -08:00
pnpm-lock.yaml chore(deps): force minimatch 11 and glob 14 for Vanta findings (#29916) 2026-11-10 12:57:20 -08:00
pnpm-workspace.yaml chore(deps): update dependency @ai-sdk/provider-utils@>=3.0.0 <3.0.38 to v3.0.45 (#29844) 2026-11-10 20:07:34 -08:00
README.md docs: fix RedisServerCache constructor usage — takes { client }, not { url } (#19569) 2026-07-16 13:35:19 +02:00

Durable Agents Example

This example demonstrates three patterns for durable agent execution, all with Redis-backed resumable streams.

Agent Patterns

Agent Factory Resumable Streams Durable Execution
durableResearchAgent createDurableAgent Redis -
eventedResearchAgent createEventedAgent Redis Workflow engine
inngestResearchAgent createInngestAgent Redis Inngest

Setup

  1. Start Redis:
docker run -d -p 6379:6379 redis
  1. Install dependencies:
pnpm install
  1. Start the dev server:
pnpm dev
  1. For Inngest agent, start the Inngest dev server:
npx inngest-cli@latest dev

Usage

Start a stream

curl -X POST http://localhost:4111/api/agents/durable-research-agent/stream \
  -H "Content-Type: application/json" \
  -d '{"messages": [{"role": "user", "content": "Research quantum computing"}]}'

Reconnect to a stream

If your connection drops, use the runId to reconnect:

curl -X POST http://localhost:4111/api/agents/durable-research-agent/observe \
  -H "Content-Type: application/json" \
  -d '{"runId": "your-run-id", "offset": 5}'

How It Works

All three agent types inherit cache and pubsub from the Mastra instance:

import { EventEmitterPubSub } from '@mastra/core/events';
import { RedisServerCache } from '@mastra/redis';
import Redis from 'ioredis';

// Redis cache for resumable streams - events persist across reconnections
const cache = new RedisServerCache({ client: new Redis('redis://localhost:6379') });

// EventEmitter pubsub for real-time delivery (process-local)
const pubsub = new EventEmitterPubSub();

export const mastra = new Mastra({
  cache,
  pubsub,
  agents: {
    durableResearchAgent, // Inherits cache/pubsub
    eventedResearchAgent, // Inherits cache/pubsub
    inngestResearchAgent, // Inherits cache/pubsub
  },
});

Events are cached with sequential indices. When observe() is called, missed events replay from cache before continuing with live events.