Skip to main content

Building AI Agents

Create AI agents that learn and improve over time with persistent memory.

Overview

AI agents benefit from memory in several ways:

  • Remember successful task completion strategies
  • Avoid repeating past mistakes
  • Build knowledge about their environment
  • Share learnings across agent instances

Task Memory Pattern

async function executeTask(task: Task, agentId: string) {
  // Recall relevant past experiences
  const results = await trix.memories.list({
    q: task.description,
    spaceId: agentId,
    tags: ['task_history'],
    limit: 5
  });

  // Build context from past experiences
  const pastLearnings = results.data.map(e => ({
    task: e.metadata.task,
    outcome: e.metadata.outcome,
    lessons: e.content
  }));

  // Execute with learned context
  const result = await agent.run(task, { pastLearnings });

  // Store outcome for future reference
  await trix.memories.create({
    content: result.lessonsLearned,
    spaceId: agentId,
    tags: ['task_history'],
    metadata: {
      task: task.description,
      outcome: result.success ? 'success' : 'failure',
      duration: result.duration
    }
  });

  return result;
}

Continuous Learning

async function learnFromOutcome(agentId: string, task: Task, outcome: Outcome) {
  if (outcome.success) {
    // Store successful strategy
    await trix.memories.create({
      content: `For tasks like "${task.type}": ${outcome.strategy}`,
      spaceId: agentId,
      tags: ['strategies'],
      metadata: { taskType: task.type, successRate: 1 }
    });
  } else {
    // Store lesson from failure
    await trix.memories.create({
      content: `Avoid for "${task.type}": ${outcome.failureReason}`,
      spaceId: agentId,
      tags: ['warnings'],
      metadata: { taskType: task.type }
    });
  }
}

This pattern enables agents to improve over time without retraining the underlying model.

Shared Agent Memory

Share learnings across multiple agent instances:

// Use a shared space for global knowledge
const SHARED_SPACE = 'agent_collective';

// Individual agent can read from shared memory
const globalKnowledge = await trix.memories.list({
  q: task.description,
  spaceId: SHARED_SPACE,
  limit: 10
});

// And contribute discoveries
await trix.memories.create({
  content: discovery,
  spaceId: SHARED_SPACE,
  metadata: { contributedBy: agentId }
});