redai — TypeScript Agent SDK

A minimal, provider-agnostic agent runtime you embed in your own Node/TypeScript apps: typed tool calling, guardrails, multi-agent handoffs, structured output, streaming, and per-run tracing — with no framework hidden underneath. Published to npm as @abhijeetkadam/redai.

@abhijeetkadam/redai — agent loop, tools, guardrails, handoffs, tracing

// Open Source / Developer Tools / AI Infrastructure · published to npm
redai: TypeScript agent SDK architecture — agent loop, tools, guardrails, handoffs, tracing
redai
No-framework agent runtime · OpenAI / Anthropic / Gemini adapters · guardrails · handoffs · structured output · tracing
  • redai is a from-scratch TypeScript agent SDK published as @abhijeetkadam/redai — no LangChain or agent framework underneath; the loop, tool dispatch, guardrails, handoffs, streaming, and tracing are all hand-written and readable in a single runner.ts.
  • Provider-agnostic core — the runner only depends on a small ModelProvider interface (generate / stream):
    • Built-in adapters for OpenAI, Anthropic, and Google Gemini as optional peer dependencies — install only what you use
    • A built-in mockProvider with no network/API key, used by examples and suitable for tests/CI
    • Swapping providers is a one-line change to Agent.model; the runner never imports vendor types
  • Three-lifetime design keeps concerns cleanly separated:
    • Agent — immutable config (instructions, model, tools, schemas, guardrails, handoffs), constructed once and reused
    • RunState — per-call turn counter and live messages for a single run()/stream() invocation
    • Session — persisted transcript across runs; the system prompt is never stored, so instructions stay owned by the agent
  • Guardrails at three stages, each able to reject or rewrite:
    • input — before the loop starts, can rewrite user text or end the run
    • tool — after Zod validation, before execute; a block becomes a recoverable tool-error message
    • output — on the final answer, before structured parsing
  • Multi-agent handoffs — each entry in agent.handoffs becomes an auto-generated transfer_to_<Agent> tool; self-handoff and chains beyond maxHandoffs raise a HandoffLoopError instead of looping forever.
  • Structured output via Zod → JSON Schema: on parse/validation failure the SDK automatically re-prompts up to maxOutputRepairAttempts before raising OutputValidationError.
  • Result types over throws — run() and stream() resolve { ok: false, error } for expected failures (guardrail violations, handoff loops, provider errors, max turns) so callers branch with instanceof instead of try/catch. Tool-level failures (bad JSON, validation, throws) are fed back to the model as recoverable messages instead of crashing the run.
  • Full observability — every run returns a Trace with runId, timing, aggregated token usage, and typed spans (model_call, tool_call, handoff, guardrail, output_repair).
  • Session persistence via a small SessionStore interface with in-memory and file-backed implementations for surviving process restarts.
  • Shipped as a real package: strict TypeScript (noUncheckedIndexedAccess, exactOptionalPropertyTypes), tsup build producing ESM + CJS + .d.ts, and a documented example suite covering guardrails, handoffs, streaming, structured output, and tracing.

// tech

TypeScriptZodzod-to-json-schematsuptsxOpenAI SDKAnthropic SDKGoogle GenAINode.js (ESM)