Back to Documentation

Agents

Build autonomous AI agents with tools, memory, and multi-agent orchestration. Create agents that can reason, use tools, and collaborate.

npm install @rana/core

Creating Agents

Build autonomous AI agents with tools and memory

import { Agent } from '@rana/core';

const agent = new Agent({
  name: 'ResearchAssistant',
  model: 'claude-sonnet-4-20250514',
  systemPrompt: `You are a helpful research assistant.
    You can search the web, read documents, and summarize findings.`,
  memory: {
    type: 'conversation',
    maxMessages: 50
  }
});

// Simple execution
const result = await agent.run('Find the latest AI research papers');

// Streaming execution
for await (const chunk of agent.stream('Summarize this article...')) {
  process.stdout.write(chunk.content);
}

Tools & Functions

Give agents access to custom tools and APIs

import { Agent, Tool } from '@rana/core';

// Define custom tools
const searchTool = new Tool({
  name: 'web_search',
  description: 'Search the web for information',
  parameters: {
    query: { type: 'string', description: 'Search query' },
    maxResults: { type: 'number', default: 5 }
  },
  handler: async ({ query, maxResults }) => {
    const results = await searchAPI.search(query, maxResults);
    return results.map(r => r.snippet).join('\n');
  }
});

const calculatorTool = new Tool({
  name: 'calculator',
  description: 'Perform mathematical calculations',
  parameters: {
    expression: { type: 'string', description: 'Math expression' }
  },
  handler: async ({ expression }) => {
    return eval(expression).toString();
  }
});

// Create agent with tools
const agent = new Agent({
  name: 'Assistant',
  model: 'claude-sonnet-4-20250514',
  tools: [searchTool, calculatorTool]
});

// Agent will automatically use tools as needed
const result = await agent.run('What is 25% of $1,234?');

Conversation Management

Multi-turn conversations with context management

import { Agent, Conversation } from '@rana/core';

const agent = new Agent({
  name: 'ChatBot',
  model: 'claude-sonnet-4-20250514'
});

// Create a conversation
const conversation = new Conversation({
  agent,
  userId: 'user-123',
  metadata: { channel: 'web' }
});

// Multi-turn conversation
await conversation.send('Hello, I need help with my order');
// "Hi! I'd be happy to help. What's your order number?"

await conversation.send('It\'s ORDER-12345');
// "Let me look that up for you..."

await conversation.send('Can you change the shipping address?');
// "Of course! What's the new address?"

// Get conversation history
const history = conversation.getHistory();

// Clear and start fresh
conversation.clear();

Multi-Agent Systems

Orchestrate multiple agents working together

import { Agent, AgentOrchestrator } from '@rana/core';

// Define specialized agents
const researcher = new Agent({
  name: 'Researcher',
  model: 'claude-sonnet-4-20250514',
  systemPrompt: 'You research and gather information.'
});

const writer = new Agent({
  name: 'Writer',
  model: 'claude-sonnet-4-20250514',
  systemPrompt: 'You write clear, engaging content.'
});

const reviewer = new Agent({
  name: 'Reviewer',
  model: 'claude-sonnet-4-20250514',
  systemPrompt: 'You review and improve content.'
});

// Create orchestrator
const orchestrator = new AgentOrchestrator({
  agents: { researcher, writer, reviewer },
  workflow: [
    { agent: 'researcher', output: 'research' },
    { agent: 'writer', input: 'research', output: 'draft' },
    { agent: 'reviewer', input: 'draft', output: 'final' }
  ]
});

// Run the workflow
const result = await orchestrator.run(
  'Write a blog post about quantum computing'
);

console.log(result.final);  // The reviewed blog post

Agent Loops

Iterative agents that refine their output

import { Agent, AgentLoop } from '@rana/core';

const agent = new Agent({
  name: 'CodeWriter',
  model: 'claude-sonnet-4-20250514',
  tools: [runTestsTool, lintTool]
});

// Create an iterative loop
const loop = new AgentLoop({
  agent,
  maxIterations: 5,
  stopCondition: async (result) => {
    // Stop when tests pass
    const testResult = await runTests(result.code);
    return testResult.passed;
  },
  onIteration: (iteration, result) => {
    console.log(`Iteration ${iteration}: ${result.status}`);
  }
});

// Run until success or max iterations
const result = await loop.run(
  'Write a function that sorts an array of objects by date'
);

console.log(result.iterations);  // How many attempts
console.log(result.code);        // Final code

Streaming & Events

Real-time streaming and event handling

import { Agent } from '@rana/core';

const agent = new Agent({
  name: 'Assistant',
  model: 'claude-sonnet-4-20250514',
  tools: [searchTool]
});

// Stream with events
const stream = agent.stream('Research AI trends', {
  onToolCall: (tool, args) => {
    console.log(`Calling tool: ${tool.name}`);
  },
  onToolResult: (tool, result) => {
    console.log(`Tool result: ${result.slice(0, 100)}...`);
  },
  onThinking: (thought) => {
    console.log(`Thinking: ${thought}`);
  }
});

// Process the stream
for await (const chunk of stream) {
  if (chunk.type === 'text') {
    process.stdout.write(chunk.content);
  } else if (chunk.type === 'tool_use') {
    console.log(`Using tool: ${chunk.tool}`);
  }
}

Agent Messaging Protocol

Type-safe pub/sub and request/response communication between agents

import { createMessageBroker, createChannel } from '@rana/agents';

// Create a message broker
const broker = createMessageBroker({
  deliveryGuarantee: 'at-least-once',
  enableRetry: true,
  maxRetries: 3
});

// Define typed channels
const taskChannel = createChannel<{ task: string; priority: number }>({
  name: 'tasks',
  type: 'topic',
  schema: { task: 'string', priority: 'number' }
});

// Subscribe to messages
broker.subscribe(taskChannel, async (message, context) => {
  console.log(`Received task: ${message.payload.task}`);
  await context.acknowledge();
});

// Publish messages with priority
await broker.publish(taskChannel, {
  task: 'Analyze user data',
  priority: 1
}, { priority: 'high' });

Request/Response Channels

Synchronous request-response patterns for agent coordination

import { createRequestChannel, createMessageBroker } from '@rana/agents';

// Create request/response channel
const queryChannel = createRequestChannel<
  { query: string },
  { results: string[]; count: number }
>({
  name: 'search-queries',
  timeout: 5000
});

// Register handler (responder agent)
broker.registerHandler(queryChannel, async (request) => {
  const results = await searchDatabase(request.payload.query);
  return {
    results: results.map(r => r.title),
    count: results.length
  };
});

// Send request and await response (requester agent)
const response = await broker.request(queryChannel, {
  query: 'AI research papers'
});

console.log(`Found ${response.count} results`);

Supported Models

Claude 3.5 Sonnet
Claude 3 Opus
GPT-4o
GPT-4 Turbo
Gemini Pro
Gemini Ultra
Llama 3
Mistral Large