Foundations
•5 min read
AI & Agents Overview
The AI adapter brings Directive's constraint system to AI agent orchestration. Wrap any LLM framework with safety guardrails, approval workflows, token budgets, and state persistence.
Architecture
Directive doesn't replace your agent framework – it wraps it:
The AI adapter is organized into five sections:
| Section | What's Inside |
|---|---|
| AI Foundations | Entry ramp – running agents, resilience, routing |
| Agent Orchestrator | Single-agent deep dive – guardrails, streaming, memory |
| Multi-Agent Orchestrator | Multi-agent deep dive – patterns, communication, goals |
| AI Infrastructure | Integrations – MCP, RAG, SSE, semantic cache |
| AI Observability | Debugging – timeline, devtools, evals, OTEL, testing |
Reading Paths
Quick Start Path
Build a working agent system step by step:
- Running Agents – End-to-end examples
- Agent Orchestrator – Single-agent with guardrails
- Multi-Agent Orchestrator – Full multi-agent setup
- Execution Patterns – Parallel, sequential, supervisor
Production Path
Harden for production after the basics:
- Resilience & Routing – Retry, fallback, budgets
- Guardrails – Input/output validation and PII detection
- Self-Healing – Automatic error recovery
- Evals – Quality measurement in CI
- OpenTelemetry – Production observability
Advanced Path
Unlock the full power of the system:
- Pattern Checkpoints – Save/resume, fork, progress tracking
- Goal Pattern – Desired-state resolution with satisfaction scoring and relaxation
- Communication – Decentralized agent messaging
- Cross-Agent State – Shared derivations and scratchpad
- Breakpoints & Checkpoints – Human-in-the-loop debugging
- DevTools – Real-time visual debugging (Timeline, Cost, State + 5 more planned)
Two Orchestrators
Both are backed by a Directive System with reactive state, constraints, guardrails, streaming, approval, memory, retry, budget, hooks, and time-travel debugging. The multi-agent orchestrator has full feature parity – each registered agent becomes a namespaced Directive module.
| Single-Agent | Multi-Agent | |
|---|---|---|
| Function | createAgentOrchestrator | createMultiAgentOrchestrator |
| Scope | One agent at a time | Multiple named agents with concurrency control |
| State | orchestrator.facts.agent | orchestrator.facts (namespaced per agent) |
| Streaming | orchestrator.runStream() | orchestrator.runAgentStream() |
| Patterns | – | parallel(), sequential(), supervisor(), dag(), race(), reflect(), debate(), goal() |
| Guardrails | Orchestrator-level | Orchestrator-level + per-agent |
| Constraints | Orchestrator-level | Orchestrator-level + per-agent |
| Approval | approve() / reject() | approve() / reject() (routes to correct agent) |
| Derivations | – | Cross-agent derivations |
| Scratchpad | – | Shared scratchpad |
| Communication | – | Message bus, agent network, handoffs |
| Breakpoints | 4 types | 6 types (+ pre_handoff, pre_pattern_step) |
totalTokens | orchestrator.totalTokens | orchestrator.totalTokens |
waitForIdle() | orchestrator.waitForIdle() | orchestrator.waitForIdle() |
| Budget warning | budgetWarningThreshold + onBudgetWarning | budgetWarningThreshold + onBudgetWarning |
| Use when | Simple chatbot, single-purpose agent | Pipelines, fan-out, delegation, routing |
// Single-agent
const single = createAgentOrchestrator({ runner, guardrails, maxTokenBudget: 10000 });
const result = await single.run(agent, 'Hello!');
// Multi-agent
const multi = createMultiAgentOrchestrator({
runner,
agents: { researcher: { agent: researcher }, writer: { agent: writer } },
guardrails,
maxTokenBudget: 50000,
});
const result = await multi.runAgent('researcher', 'What is WASM?');
Key Concepts
| Concept | Description |
|---|---|
| Orchestrator | Wraps an AgentRunner with constraints, guardrails, and state tracking |
| Multi-Agent Orchestrator | Coordinates multiple named agents with concurrency, patterns, and per-agent configuration |
pipe() | Left-to-right middleware composition: pipe(runner, withRetry(...), withBudget(...)) |
| Middleware | Composable with* wrappers (withRetry, withFallback, withBudget) |
| Guardrails | Input, output, and tool-call validators that block or transform data |
| Constraints | Declarative rules (e.g., "if confidence < 0.7, escalate to expert") |
| Memory | Sliding window, token-based, or hybrid conversation management |
| Resilience | Intelligent retry, provider fallback chains, and cost budget guards |
| Circuit Breaker | Automatic fault isolation for failing agent calls |
| Goal Pattern | Desired-state goal resolution – declare produces/requires, runtime resolves |
| Checkpoints | Save/resume mid-pattern state for fault tolerance, forking, and progress tracking |
| Evals | Dataset-driven quality evaluation with built-in and LLM-as-judge criteria |
| DevTools | Real-time debugging UI with 12 specialized views |
Quick Example
import { createAgentOrchestrator, createPIIGuardrail } from '@directive-run/ai';
import { createOpenAIRunner } from '@directive-run/ai/openai';
const runner = createOpenAIRunner({
apiKey: process.env.OPENAI_API_KEY!,
});
const agent = {
name: 'assistant',
instructions: 'You are a helpful assistant.',
};
const orchestrator = createAgentOrchestrator({
runner,
// Block PII in user input
guardrails: {
input: [createPIIGuardrail()],
},
// Auto-enforce a 10K token budget
maxTokenBudget: 10000,
// React to agent state with declarative rules
constraints: {
escalateOnLowConfidence: {
when: (facts) => facts.agent.output?.confidence < 0.7,
require: (facts) => ({
type: 'RUN_EXPERT_AGENT',
query: facts.agent.input,
}),
},
},
});
const result = await orchestrator.run(agent, 'Hello!');
Infrastructure
| Feature | Page | Description |
|---|---|---|
| MCP Integration | Model Context Protocol | Connect to MCP tool servers |
| RAG Enricher | Retrieval-Augmented Generation | Embedding-based document retrieval |
| SSE Transport | Server-Sent Events | HTTP streaming endpoints |
| Semantic Cache | Response Caching | Embedding-based cache with ANN indexes |
Observability
| Feature | Page | Description |
|---|---|---|
| Debug Timeline | Event Recording | 25+ event types with time-travel correlation |
| Pattern Checkpoints | Fault Tolerance | Save/resume all 8 patterns, progress tracking, forking |
| Breakpoints & Checkpoints | Pausing & State | Human-in-the-loop debugging, persistent snapshots |
| DevTools | Visual Debugging | 12 views: Timeline, Cost, State, Guardrails, Events, Health, Flamechart, DAG, Goal, Memory, Budget, Config |
| Evals | Quality Measurement | 10 built-in criteria, LLM-as-judge, CI assertions |
| OpenTelemetry | Production Tracing | OTEL spans with GenAI semantic conventions |
| Testing | Test Utilities | Mock runners, test orchestrators, assertion helpers |
Safety & Compliance
Directive provides security guardrails and compliance tooling for AI agent systems. See Security & Compliance for full details.
| Feature | Threat Addressed |
|---|---|
| PII Detection | Personal information leaking to/from agents |
| Prompt Injection | Jailbreaks, instruction overrides |
| Audit Trail | Tamper-evident logging |
| GDPR/CCPA | Right to erasure, data export, consent |
Next Steps
- New to AI adapter? Start with Running Agents
- Need resilience? See Resilience & Routing
- Want streaming? See Streaming
- Need safety? See Guardrails and Security & Compliance
- Production debugging? See DevTools and Evals

