/directive:building-ai-agentsfrom directive
Configures AI provider runners (Anthropic, OpenAI, Ollama), handles token streaming and structured output, and wires cross-agent communication.
Ships in directive. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
This skill applies when the project uses @directive-run/ai. If not found in package.json, suggest installing it: npm install @directive-run/ai.
createAnthropicRunner, createOpenAIRunner, createOllamaRunnerbuilding-ai-orchestratorshardening-ai-systemstesting-ai-systemsChoosing a provider?
├── Production, best quality → createAnthropicRunner (claude-opus-4-6)
├── Production, cost-efficient → createAnthropicRunner (claude-haiku-4-5)
├── OpenAI ecosystem / GPT models → createOpenAIRunner
├── Local / private / no API key → createOllamaRunner
└── Custom provider → implement AgentRunner interface
Need the response immediately as it arrives?
├── Yes, show tokens as they generate → runStream() + "token" events
├── Yes, but structured JSON → runStream() + "structured" events
└── No, wait for full response → run()
import { createAnthropicRunner } from "@directive-run/ai/anthropic";
const runner = createAnthropicRunner({
model: "claude-opus-4-6", // or "claude-haiku-4-5", "claude-sonnet-4-6"
apiKey: process.env.ANTHROPIC_API_KEY,
maxTokens: 4096, // optional, default: 1024
temperature: 0.7, // optional, default: 1.0
systemPrompt: "You are helpful.", // optional default system prompt
});
import { createOpenAIRunner } from "@directive-run/ai/openai";
const runner = createOpenAIRunner({
model: "gpt-4o", // or "gpt-4o-mini", "gpt-3.5-turbo"
apiKey: process.env.OPENAI_API_KEY,
maxTokens: 4096,
temperature: 0.7,
organization: "org-...", // optional
});
import { createOllamaRunner } from "@directive-run/ai/ollama";
const runner = createOllamaRunner({
model: "llama3.2", // any Ollama-installed model
baseUrl: "http://localhost:11434", // default Ollama endpoint
temperature: 0.8,
});
resolvers: {
generate: {
requirement: "GENERATE",
resolve: async (req, context) => {
// Basic call – returns full response when complete
const result = await context.runner.run({
prompt: "Explain quantum entanglement simply.",
system: "You are a science communicator.",
maxTokens: 500,
});
context.facts.output = result.text;
context.facts.tokenUsage = result.usage.totalTokens;
},
},
},
interface RunnerResult {
text: string; // Generated text
usage: {
inputTokens: number;
outputTokens: number;
totalTokens: number;
};
model: string; // Model used
stopReason: "end_turn" | "max_tokens" | "stop_sequence";
raw: unknown; // Raw provider response
}
// orchestrator.runStream() returns AsyncIterable<OrchestratorEvent>
const stream = orchestrator.runStream({ input: "Write a story about..." });
for await (const event of stream) {
if (event.type === "token") {
// Arrives as the LLM generates – display immediately
process.stdout.write(event.text);
}
if (event.type === "done") {
console.log("\nFinal facts:", event.facts);
}
if (event.type === "error") {
console.error("Stream error:", event.error.message);
break;
}
}
type OrchestratorEvent =
| { type: "token"; text: string; agentId?: string }
| { type: "requirement_met"; requirementType: string; agentId?: string }
| { type: "requirement_queued"; requirementType: string }
| { type: "agent_started"; agentId: string }
| { type: "agent_done"; agentId: string }
| { type: "done"; facts: Record<string, unknown> }
| { type: "error"; error: Error };
import { z } from "zod";
// When you need typed JSON output, not raw text
const result = await context.runner.runStructured({
prompt: "Extract product info from this description: ...",
schema: z.object({
name: z.string(),
price: z.number(),
inStock: z.boolean(),
}),
});
// result.data is typed to the schema
context.facts.productName = result.data.name;
context.facts.price = result.data.price;
resolve: async (req, context) => {
// Emit custom events consumers can observe
context.emit({ type: "token", text: "Starting analysis..." });
const result = await context.runner.run({ prompt: req.input });
context.emit({ type: "token", text: result.text });
context.facts.output = result.text;
},
In createMultiAgentOrchestrator, agents share coordinator facts. The coordinator acts as the message bus.
// researcher agent writes to coordinator facts
resolve: async (req, context) => {
const result = await context.runner.run({ prompt: "Research AI trends..." });
// Writing to shared coordinator facts signals the next agent
context.facts.researchComplete = true;
context.facts.researchNotes = result.text;
},
// writer agent reads coordinator facts set by researcher
constraints: {
startWriting: {
// Only activates after researcher signals completion
when: (facts) => facts.researchComplete && !!facts.researchNotes,
require: { type: "WRITE_ARTICLE" },
},
},
// Pass structured data in the requirement itself
constraints: {
routeToSpecialist: {
when: (facts) => facts.analysisType === "legal",
require: (facts) => ({
type: "SPECIALIST_REVIEW",
category: facts.analysisType,
priority: facts.urgency,
content: facts.draftOutput,
}),
},
},
resolvers: {
specialistReview: {
requirement: "SPECIALIST_REVIEW",
resolve: async (req, context) => {
// req contains { category, priority, content } from above
const result = await context.runner.run({
prompt: `Review this ${req.category} content: ${req.content}`,
});
context.facts.reviewedOutput = result.text;
},
},
},
import type { AgentRunner, RunOptions, RunnerResult } from "@directive-run/ai";
class MyCustomRunner implements AgentRunner {
async run(options: RunOptions): Promise<RunnerResult> {
// Call your provider
const response = await myProviderClient.generate({
prompt: options.prompt,
system: options.system,
});
return {
text: response.text,
usage: {
inputTokens: response.usage.input,
outputTokens: response.usage.output,
totalTokens: response.usage.total,
},
model: "my-custom-model",
stopReason: "end_turn",
raw: response,
};
}
// Optional: implement for streaming support
async *runStream(options: RunOptions): AsyncIterable<string> {
for await (const chunk of myProviderClient.stream(options)) {
yield chunk.text;
}
}
}
// Maintain conversation history in facts
factsSchema: {
messages: t.array(t.object<{ role: "user" | "assistant"; content: string }>()),
latestInput: t.string(),
latestOutput: t.string().optional(),
},
init: (facts) => {
facts.messages = [];
},
resolve: async (req, context) => {
// Add user message to history
context.facts.messages.push({
role: "user",
content: context.facts.latestInput,
});
const result = await context.runner.run({
messages: context.facts.messages, // Pass full history
system: "You are a helpful assistant.",
});
// Add assistant reply to history
context.facts.messages.push({
role: "assistant",
content: result.text,
});
context.facts.latestOutput = result.text;
},
// WRONG – if stream throws, it crashes uncaught
for await (const event of orchestrator.runStream(input)) {
if (event.type === "token") {
process.stdout.write(event.text);
}
}
// CORRECT – always handle error events
for await (const event of orchestrator.runStream(input)) {
if (event.type === "token") {
process.stdout.write(event.text);
}
if (event.type === "error") {
console.error("Stream failed:", event.error.message);
break;
}
}
// WRONG – consuming a stream inside a resolver blocks the engine
resolve: async (req, context) => {
const stream = orchestrator.runStream(req.input);
for await (const event of stream) { /* deadlock risk */ }
},
// CORRECT – use context.runner.run() inside resolvers
resolve: async (req, context) => {
const result = await context.runner.run({ prompt: req.input });
context.facts.output = result.text;
},
Always use (req, context) – never (req, ctx) or (request, context).
// WRONG
const runner = createAnthropicRunner({ apiKey: "sk-ant-..." });
// CORRECT
const runner = createAnthropicRunner({ apiKey: process.env.ANTHROPIC_API_KEY });
// WRONG – fire and forget, no budget awareness
const result = await context.runner.run({ prompt });
context.facts.output = result.text;
// CORRECT – track usage for budget management
const result = await context.runner.run({ prompt });
context.facts.output = result.text;
context.facts.tokensUsed = (context.facts.tokensUsed ?? 0) + result.usage.totalTokens;
ai-agents-streaming.md – Streaming events, AsyncIterable patterns, token bufferingai-adapters.md – Provider adapter interface, all runner options, custom runner guideai-communication.md – Cross-agent communication, coordinator facts, requirement payloadsWhat SKILL.md sets; muted values are Claude Code's defaults
Other plugins' skills for the same job
npx claudepluginhub directive-run/directive --plugin directive12 skills in this plugin
/building-ai-orchestratorsCreate AI orchestration systems using createAgentOrchestrator (single agent) and createMultiAgentOrchestrator (multi-agent). Define factsSchema with t.*() builders, init state, constraints for trigger conditions, and resolvers for LLM execution. Use when building LLM-powered workflows, agent pipelines, task runners, or multi-agent coordination systems./building-directive-systemsCompose Directive modules into systems: single-module and multi-module createSystem() calls, namespaced fact/derive/event access, cross-module dependencies, initialFacts and hydration, plugins (logging, devtools, persistence, circuit breaker), React adapter hooks (useSelector, useEvent, useSystem, DirectiveProvider), and system lifecycle. Use when creating a system, connecting modules together, adding plugins, or integrating Directive with React./getting-started-with-directiveUnderstand Directive fundamentals: what modules, facts, derivations, constraints, resolvers, and systems are, and how they fit together. Use when someone is new to Directive, asks 'what is Directive', wants to understand the mental model, or needs help choosing between Directive concepts./hardening-ai-systemsAdd guardrails (input/output validation), memory strategies, token budgets, PII detection, prompt injection prevention, and circuit breakers to AI orchestrators. Use when enforcing content policies, managing costs, preventing security vulnerabilities, adding resilience, or implementing agent memory across turns./migrating-to-directiveMigrate state management code from Redux, Zustand, XState, MobX, Recoil, or Jotai to Directive. Provides concept mapping, step-by-step migration patterns, and before/after code examples. Use when asked to migrate, convert, or port existing state management to Directive./reviewing-directive-codeReview Directive code for anti-patterns, naming violations, missing error boundaries, constraint/resolver misuse, and performance issues. Use when asked to review, audit, or improve existing Directive modules, systems, or orchestrators.