Graphorin API reference v0.1.0
Graphorin API reference / @graphorin/agent
@graphorin/agent
Agent runtime for the Graphorin framework.
@graphorin/agent is the agent layer of the Graphorin framework. It owns the typed model -> tool calls -> model loop, the streaming event surface, durable HITL approvals, multi-agent handoffs, and the cross-cutting safety mechanisms (intra-loop reasoning preservation, agent-level model fallback, post-compaction hooks, per-tool model-tier hints, lateral-leak defenses).
The package is library-mode-first: every primitive that can be useful from a script (createAgent, RunState.toJSON / fromJSON, the filter library, evaluatorOptimizer, Agent.fanOut, agent.progress.write / read) ships from the npm package without the optional standalone server.
Dependencies
@graphorin/core— typed contracts (AgentEvent,RunState,RunContext,Provider,Tool,StopCondition,HandoffFilter,Sensitivity, …).@graphorin/provider— middleware composer assertion + token counter dispatcher + per-provider model-tier auto-classifier.@graphorin/tools—ToolRegistry+ToolExecutor(parallel / sequential dispatch, approval flow, sandbox enforcement, inbound sanitization).@graphorin/skills— skill metadata cards, lazy body loading.@graphorin/memory—Memoryfacade +ContextEnginefor per-step prompt assembly + auto-compaction trigger.@graphorin/sessions— multi-agent attribution, handoff records,AgentRegistry, audit trail.@graphorin/observability—Tracer, span attributes, counter receivers.@graphorin/security—SecretsAccessScopefor sub-agent inheritance, audit chain, lateral-leak guard primitives.
Quick start
import { createAgent } from '@graphorin/agent';
import { createProvider } from '@graphorin/provider';
const agent = createAgent({
name: 'helpful-assistant',
instructions: 'You are a helpful, concise assistant.',
provider: createProvider({ /* ... */ }),
});
for await (const event of agent.stream('Plan a trip to Mars')) {
if (event.type === 'text.delta') process.stdout.write(event.delta);
}Highlights
- Streaming-first. Every operation returns
AsyncIterable<AgentEvent<TOutput>>.agent.run(...)is a thin collect helper. - Typed events. The
AgentEvent<TOutput>discriminated union covers every event the runtime emits (text deltas, tool calls, approvals, handoffs, compaction, fan-out, evaluator iterations, progress artifacts, lateral-leak detections, model fallback transitions, …).assertNever(...)exhaustiveness is verified at compile time. - Durable HITL.
RunState.toJSON()/RunState.fromJSON(serialized, agent)round-trip the full run state through any storage the caller picks (file, SQLite, KV, S3). - Multi-agent.
Agent.toTool({ exposeTurns, secretsInheritance, inheritSecrets, inputFilter })wraps an agent as a tool the parent agent can call. The defaultsecretsInheritance: 'inherit-allowlist'with emptyinheritedSecretsenforces the principle of least authority. - Filter library.
filters.lastN(n),filters.lastUser,filters.summary({...}),filters.bySensitivity({...}),filters.stripReasoning(),filters.stripSensitiveOutputs(),filters.stripToolCalls(),filters.compose(...). Every filter returns a serializableHandoffInputFilterDescriptorso the JSONL session export can replay it byte-equal. - Cancellation.
agent.abort({ drain, onPendingApprovals })— hard-kill default with 50 ms grace;drain: truewaits for the current step to complete; pending approvals can be auto-denied ('deny'default), held ('hold'), or raised as an error ('fail'). - Reasoning preservation. Anthropic Claude tool-use loops round-trip
reasoningcontent parts (with opaquemetasuch assignature/data) into the next provider call when the effectivereasoningRetentionis not'strip'. The handoff boundary is independent:filters.stripReasoning()is always applied to messages forwarded to a sub-agent regardless of the intra-loop policy. - Agent-level model fallback.
Agent.fallbackModels: ModelSpec[]retries the whole step against the next model on rate-limit / capacity / context-length errors;agent.model.fellbackevent fires per transition; per-model usage attribution lives inRunState.usage.byModel. - Post-compaction hook lifecycle. When
@graphorin/memory.contextEngineauto-compacts the buffer, the runtime fires every registeredpostCompactionHooks[i]between the trim and the nextprovider.stream(...)call. Failed hooks are isolated; the harness continues with the survivors. - Agent-step-level fan-out.
Agent.fanOut({ subagents, mergeStrategy, perBudget, ... })spawns N sub-agents under a bounded-fanout cap (defaultmaxConcurrentChildren: 4) with per-child token / tool-call / duration budgets and four built-in merge strategies ('concat','first-success','judge-merge','custom'). evaluatorOptimizer({...}). Generator → Evaluator iteration loop with three rubric kinds ('free-form','zod','llm-judge') + REQUIRED iteration cap.- Progress artifacts.
agent.progress.write(content, { role, seq, sensitivity, tags })andagent.progress.read({ runId, role, sinceSeq, maxArtifacts })persist UTF-8 text artifacts to the artifact root via atomic-write.tmp + renamediscipline for cross-session continuity. - Per-tool model-tier hints.
tool({ ..., preferredModel: 'fast' | 'balanced' | 'smart' | ModelSpec })declares a hint;Agent.modelTierMapresolves the cost-tier vocabulary to concreteProviderinstances at agent warm-up; the per-step planner walks the precedence ladder'prepare-step' > 'tier-map' | 'spec' > 'agent-preferred' > 'fallthrough-default'once per step. - Lateral-leak defense layer.
Agent.causalityMonitor(Agentic Reference Monitor pattern),Agent.mergeGuard(per-child trust scoring + bias detection on'judge-merge'), the protocol injection guard (control-character escape catalogue), and commentary-phase trace sanitization at the session-output boundary compose orthogonally with the other security layers (sub-agent secrets isolation, handoff input filter, outbound redaction, inbound sanitization). - Inbound sanitization preamble. When the assembled message list contains any non-trusted
MessageContentpart, the runtime appends the locale-resolved preamble fragment to the system prompt after the cache breakpoint so the trusted-only cache prefix is not invalidated.
Documentation
The full architecture lives in the framework docs (the agent loop reference). The package's CHANGELOG.md records every change.
License
MIT © Oleksiy Stepurenko. See LICENSE for the full text.
Project Graphorin · v0.1.0 · MIT License · © 2026 Oleksiy Stepurenko · https://github.com/o-stepper/graphorin
Modules
| Module | Description |
|---|---|
@graphorin/agent — agent runtime for the Graphorin framework. | |
| errors | Typed error surface for @graphorin/agent. |
| evaluator-optimizer | evaluatorOptimizer({...}) — Generator → Evaluator iteration loop with three rubric kinds and a REQUIRED iteration cap. |
| factory | createAgent({...}) — the agent factory entry point. |
| fallback | Agent-level model fallback chain primitives. |
| fanout | Agent-step-level fan-out — Agent.fanOut(...) convenience that spawns N sub-agents in parallel under a bounded-fanout cap with per-child budgets and four built-in merge strategies. |
| filters | Handoff filter library — a small set of pure, composable functions that take the parent agent's message history and return a filtered subset suitable for forwarding to a child agent. |
| lateral-leak | Lateral-leak defense layer aggregate exports. |
| preferred-model | Per-tool / per-agent preferred-model resolution. Pure functions consulted by the agent loop AFTER the model has decided which tool(s) to call but BEFORE provider.stream(...) is invoked. |
| progress | Structured progress-artifact IO. Persists UTF-8 text artifacts under <artifactRoot>/<runId>/progress/<role>.<seqPadded>.txt via atomic-write .tmp + rename discipline. |
| run-state | RunState JSON serialization and rehydration. |