IntegrationsUpdated 2026-03-18
Agent Runtime Integration
integrate RetainDB with multi-agent systems and tool-heavy runtimes for stateful conversations.
Applies to: Multi-agent runtimes
Learn how to integrate RetainDB with complex agent runtimes.
Overview
Agent runtimes require special handling for:
- Multi-agent conversations
- Tool execution tracking
- Session state management
- Idempotent operations
Session Management
Start Session
typescript
// When agent starts
await client.session.start({
user_id: "user@example.com",
session_id: "agent-session-123",
metadata: {
agent_type: "assistant",
version: "1.0",
},
});Track Events
typescript
// User message
await client.session.event({
user_id: "user@example.com",
session_id: "agent-session-123",
type: "user_message",
content: "I need help with coding",
});
// Tool execution
await client.session.event({
user_id: "user@example.com",
session_id: "agent-session-123",
type: "tool_execution",
content: "search_docs",
event_data: {
tool: "search",
input: "how to use RetainDB API",
output: "Found 5 results",
},
});
// Agent response
await client.session.event({
user_id: "user@example.com",
session_id: "agent-session-123",
type: "agent_message",
content: "Here's what I found...",
});Idempotent Operations
Use deterministic IDs for idempotency:
typescript
// Generate deterministic event ID
const eventId = `${sessionId}-${timestamp}-${eventType}`;
await client.session.event({
user_id: "user@example.com",
session_id: "agent-session-123",
event_id: eventId, // For idempotency
type: "tool_execution",
content: "search",
});Tool Memory Integration
Store tool results as memories:
typescript
async function handleToolCall(toolName: string, input: any, output: any) {
// Store tool result
await client.memory.add({
user_id: userId,
session_id: sessionId,
content: `Tool: ${toolName}\nInput: ${JSON.stringify(input)}\nOutput: ${JSON.stringify(output)}`,
memory_type: "event",
metadata: {
type: "tool_execution",
tool: toolName,
},
});
}Multi-Agent Scenarios
Agent-to-Agent Communication
typescript
// Agent A responds
await client.session.event({
user_id: "user@example.com",
session_id: "agent-session-123",
type: "agent_message",
content: "Let me check with the coding assistant",
metadata: {
from_agent: "assistant-a",
to_agent: "assistant-b",
},
});
// Agent B responds
await client.session.event({
user_id: "user@example.com",
session_id: "agent-session-123",
type: "agent_message",
content: "The RetainDB API has these endpoints...",
metadata: {
from_agent: "assistant-b",
},
});Complete Agent Example
typescript
import { RetainDBClient } from "@retaindb/sdk";
const client = RetainDBClient.fromEnv();
class RetainDBAgentRuntime {
constructor(private userId: string) {}
async startSession(sessionId: string) {
await client.session.start({
user_id: this.userId,
session_id: sessionId,
metadata: {
started_at: new Date().toISOString(),
},
});
}
async handleMessage(message: string, response: string, tools: any[] = []) {
const sessionId = this.getCurrentSession();
// Track user message
await client.session.event({
user_id: this.userId,
session_id: sessionId,
type: "user_message",
content: message,
});
// Store as conversation memory
await client.memory.add({
user_id: this.userId,
session_id: sessionId,
content: `User: ${message}\nAssistant: ${response}`,
memory_type: "event",
});
// Track tool executions
for (const tool of tools) {
await client.session.event({
user_id: this.userId,
session_id: sessionId,
type: "tool_execution",
content: tool.name,
event_data: tool,
});
}
}
async endSession() {
await client.session.end({
user_id: this.userId,
session_id: this.getCurrentSession(),
retain_context: true,
});
}
private getCurrentSession() {
// Get current session ID
return `session-${Date.now()}`;
}
}Best Practices
- Always start/end sessions - Proper lifecycle management
- Use deterministic IDs - For idempotent operations
- Track tool executions - For debugging and context
- Store context in memory - For retrieval
Next step
Was this page helpful?
Your feedback helps us prioritize docs improvements weekly.