# syrin-labs/syrin-harness architecture

> Architecture diagram of the GitHub repository syrin-labs/syrin-harness, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-23). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/syrin-labs/syrin-harness
- Repository: https://github.com/syrin-labs/syrin-harness

## Overview

1. **Repository shape**
   - This is a Python package (`src/syrin`) with a large set of runnable examples, docs, and tests.
   - The package is organized as a production AI harness: a central `Agent` abstraction wraps model calls with budget, memory, guardrails, observability, sandboxing, and multi-agent orchestration.
   - There is also a small TypeScript/Next.js `playground/` app that acts as a UI boundary for serving and debugging agents.

2. **Primary runtime boundary: `Agent` as the orchestration core**
   - The main execution path starts in `syrin.agent` and related internal modules.
   - `Agent.run()` / async run flows through context building, prompt construction, model/provider resolution, tool execution, budget checks, guardrails, and response assembly.
   - Most internal modules are split into private implementation files (`_core`, `_run`, `_tool_exec`, `_context_builder`, etc.), which keeps the public API thin and the execution pipeline centralized.

3. **Model/provider layer**
   - `src/syrin/model` and `src/syrin/providers` isolate model abstraction from vendor specifics.
   - Providers include OpenAI, Anthropic, OpenRouter, LiteLLM, Ollama, and a mock provider for testing.
   - `router/` adds model selection and modality-aware routing, so the agent can switch between models/providers based on cost, capability, or input type.

4. **Budgeting and cost control**
   - `budget/`, `cost/`, `ratelimit/`, and `budget_store/` implement the cost-control subsystem.
   - The architecture supports preflight estimation, live enforcement, threshold callbacks, forecast/history, shared pools, and rate windows.
   - Budget enforcement is not passive telemetry; it is a runtime gate that can stop, warn, or switch execution before another model call happens.

5. **Memory and knowledge are separate retrieval systems**
   - `memory/` manages persistent agent memory with backends such as SQLite, Postgres, Redis, Chroma, and Qdrant.
   - `knowledge/` is the RAG/document-integration layer: loaders ingest files and external sources, chunkers split content, stores persist embeddings/metadata, and grounding/verification support answer validation.
   - The architecture keeps conversational memory, long-term user facts, and knowledge retrieval distinct so they can evolve independently.

6. **Guardrails, security, and human-in-the-loop**
   - `guardrails/`, `security/`, and `hitl/` form the safety boundary around model behavior.
   - Guardrails cover PII, content, length, citations, fact verification, authority/capability checks, and prompt-injection defenses.
   - HITL adds approval gates and session management where an agent needs explicit human confirmation before continuing.

7. **Multi-agent orchestration**
   - `swarm/`, `workflow/`, `rlm/`, `task/`, `agent_router.py`, and `agent/pipeline.py` describe the multi-agent layer.
   - The repo supports several coordination patterns: orchestrator, parallel, consensus, reflection, workflows, handoff, broadcast, spawn, and recursive decomposition.
   - Shared budget pools, registry/pool abstractions, and memory buses provide the cross-agent control plane.

8. **Sandboxed execution**
   - `sandbox/` isolates code execution in subprocesses rather than the main Python process.
   - The sandbox supports Python and bash execution, working directories/files, package preinstallation, timeout limits, and memory caps.
   - This subsystem is the boundary between LLM-generated instructions and real execution, which is important for safety and reproducibility.

9. **Serving and remote interfaces**
   - `serve/`, `remote/`, `remote_config/`, and `mcp/` expose agents as services and interoperable endpoints.
   - The package supports HTTP, CLI, and stdio serving, plus a bundled playground UI via static assets.
   - MCP client/server support indicates the repo is also intended to integrate with external tool ecosystems and remote command/config negotiation.

10. **Observability, debugging, and replay**
    - `observability/`, `audit/`, `logging/`, `debug/`, and `hooks.py` provide tracing and event instrumentation.
    - The architecture emits typed lifecycle hooks across agent runs, tools, memory, budget events, sandbox execution, and swarm actions.
    - A key design idea is that execution is observable without monkey-patching: the same events can feed logs, metrics, traces, audits, or a live debug UI.

11. **Context, prompts, outputs, and types**
    - `context/`, `prompt/`, `template.py`, `output_format/`, `response/`, `validation.py`, and `types/` manage the data shaping layer.
    - Context assembly, compaction, runtime injection, structured outputs, and output-format conversion all sit between raw user input and model/provider calls.
    - These modules are the main boundary where unstructured chat input becomes typed agent state and typed responses.

12. **Extensibility surface**
    - `tool/`, `capabilities/`, `generation/`, `embedding/`, `multimodal/`, `watch/`, and `resource/` are feature subsystems that plug into the core agent runtime.
    - Tools are schema-driven, generation covers text/voice/media output, embeddings support memory/knowledge/routing, watches add triggers like cron/webhook/queue, and resource limits control run-time degradation.
    - The repository is designed so new capabilities attach to the same agent execution pipeline rather than creating separate frameworks.

13. **Tests and examples mirror the architecture**
    - `tests/` is organized by subsystem and includes unit, integration, contract, e2e, and stress coverage.
    - `examples/` maps almost one-to-one to the main architecture areas: budget, memory, tools, loops, swarm, streaming, guardrails, observability, context, MCP, serving, routing, multimodal, knowledge, and checkpoints.
    - For an architecture diagram, these examples are useful as concrete usage nodes, while the tests define the intended subsystem boundaries and invariants.

Main entry points: [`src/syrin/agent/_core.py`](https://github.com/syrin-labs/syrin-harness/blob/main/src/syrin/agent/_core.py) (Run Core), [`src/syrin/serve/http.py`](https://github.com/syrin-labs/syrin-harness/blob/main/src/syrin/serve/http.py) (Serve), [`src/syrin/observability/_core.py`](https://github.com/syrin-labs/syrin-harness/blob/main/src/syrin/observability/_core.py) (Observability), [`src/syrin/swarm/_core.py`](https://github.com/syrin-labs/syrin-harness/blob/main/src/syrin/swarm/_core.py) (Swarm)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_core["Core Runtime"]
  node_agent_api["Agent API<br/>public agent facade<br/>[__init__.py]"]
  node_agent_core["Run Core<br/>execution pipeline<br/>[_core.py]"]
  node_context_prompt["Context & Prompt<br/>shaping layer"]
  node_tool_exec["Tool Exec<br/>tool runtime<br/>[_tool_exec.py]"]
  node_model_provider["Model Layer<br/>model/provider bridge<br/>[_model_provider.py]"]
end

subgraph group_control["Safety & Control"]
  node_budget_control["Budget Control<br/>cost gate<br/>[_core.py]"]
  node_rate_limit["Rate Limits<br/>traffic gate<br/>[_manager.py]"]
  node_guardrails["Guardrails<br/>safety engine<br/>[engine.py]"]
  node_hitl["HITL Gate<br/>approval gate<br/>[_gate.py]"]
end

subgraph group_state["State & Retrieval"]
  node_memory[("Memory<br/>[store.py]")]
  node_knowledge[("Knowledge RAG<br/>document retrieval<br/>[_core.py]")]
end

subgraph group_orchestration["Multi-Agent"]
  node_swarm["Swarm<br/>agent coordination<br/>[_core.py]"]
  node_workflow["Workflow<br/>step executor<br/>[_core.py]"]
  node_router["Router<br/>model routing<br/>[router.py]"]
end

subgraph group_interfaces["Interfaces"]
  node_serve["Serve<br/>service boundary<br/>[http.py]"]
  node_remote["Remote Config<br/>control plane<br/>[_core.py]"]
  node_mcp["MCP<br/>interop endpoint<br/>[server.py]"]
  node_playground["Playground UI<br/>Next.js ui<br/>[page.tsx]"]
end

subgraph group_telemetry["Observability"]
  node_observability["Observability<br/>telemetry layer<br/>[_core.py]"]
  node_debug["Debug UI<br/>inspection tools<br/>[_ui.py]"]
end

node_agent_api -->|"runs"| node_agent_core
node_agent_core -->|"builds"| node_context_prompt
node_agent_core -->|"calls"| node_model_provider
node_agent_core -->|"executes"| node_tool_exec
node_agent_core -->|"checks"| node_budget_control
node_agent_core -->|"throttles"| node_rate_limit
node_agent_core -->|"filters"| node_guardrails
node_agent_core -->|"escalates"| node_hitl
node_agent_core -->|"reads/writes"| node_memory
node_agent_core -->|"grounds"| node_knowledge
node_router -->|"selects"| node_model_provider
node_swarm -->|"orchestrates"| node_agent_core
node_workflow -->|"steps"| node_agent_core
node_swarm -->|"routes"| node_router
node_workflow -->|"budgets"| node_budget_control
node_serve -->|"exposes"| node_agent_api
node_serve -->|"negotiates"| node_remote
node_serve -->|"bridges"| node_mcp
node_serve -->|"ships"| node_playground
node_observability -->|"instruments"| node_agent_core
node_observability -->|"traces"| node_swarm
node_debug -->|"consumes"| node_observability
node_guardrails -->|"emits"| node_observability
node_memory -.->|"complements"| node_knowledge

click node_agent_api "https://github.com/syrin-labs/syrin-harness/blob/main/src/syrin/agent/__init__.py"
click node_agent_core "https://github.com/syrin-labs/syrin-harness/blob/main/src/syrin/agent/_core.py"
click node_context_prompt "https://github.com/syrin-labs/syrin-harness/blob/main/src/syrin/agent/_context_builder.py"
click node_tool_exec "https://github.com/syrin-labs/syrin-harness/blob/main/src/syrin/agent/_tool_exec.py"
click node_model_provider "https://github.com/syrin-labs/syrin-harness/blob/main/src/syrin/agent/_model_provider.py"
click node_budget_control "https://github.com/syrin-labs/syrin-harness/blob/main/src/syrin/budget/_core.py"
click node_rate_limit "https://github.com/syrin-labs/syrin-harness/blob/main/src/syrin/ratelimit/_manager.py"
click node_guardrails "https://github.com/syrin-labs/syrin-harness/blob/main/src/syrin/guardrails/engine.py"
click node_hitl "https://github.com/syrin-labs/syrin-harness/blob/main/src/syrin/hitl/_gate.py"
click node_memory "https://github.com/syrin-labs/syrin-harness/blob/main/src/syrin/memory/store.py"
click node_knowledge "https://github.com/syrin-labs/syrin-harness/blob/main/src/syrin/knowledge/_core.py"
click node_swarm "https://github.com/syrin-labs/syrin-harness/blob/main/src/syrin/swarm/_core.py"
click node_workflow "https://github.com/syrin-labs/syrin-harness/blob/main/src/syrin/workflow/_core.py"
click node_router "https://github.com/syrin-labs/syrin-harness/blob/main/src/syrin/router/router.py"
click node_serve "https://github.com/syrin-labs/syrin-harness/blob/main/src/syrin/serve/http.py"
click node_remote "https://github.com/syrin-labs/syrin-harness/blob/main/src/syrin/remote/_core.py"
click node_mcp "https://github.com/syrin-labs/syrin-harness/blob/main/src/syrin/mcp/server.py"
click node_playground "https://github.com/syrin-labs/syrin-harness/blob/main/playground/app/page.tsx"
click node_observability "https://github.com/syrin-labs/syrin-harness/blob/main/src/syrin/observability/_core.py"
click node_debug "https://github.com/syrin-labs/syrin-harness/blob/main/src/syrin/debug/_ui.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_agent_api,node_agent_core,node_context_prompt,node_tool_exec,node_model_provider toneBlue
class node_budget_control,node_rate_limit,node_guardrails,node_hitl toneAmber
class node_memory,node_knowledge toneMint
class node_swarm,node_workflow,node_router toneRose
class node_serve,node_remote,node_mcp,node_playground toneIndigo
class node_observability,node_debug toneTeal
```

## Components

### Core Runtime

Agent execution pipeline and shared shaping layers.

- [Agent API](https://github.com/syrin-labs/syrin-harness/blob/main/src/syrin/agent/__init__.py): public agent facade · Thin public entrypoint for constructing and running agents. · `src/syrin/agent/__init__.py`
- [Run Core](https://github.com/syrin-labs/syrin-harness/blob/main/src/syrin/agent/_core.py): execution pipeline · Central run loop that coordinates prompt building, model calls, tools, and response assembly. · `src/syrin/agent/_core.py`
- [Context & Prompt](https://github.com/syrin-labs/syrin-harness/blob/main/src/syrin/agent/_context_builder.py): shaping layer · Builds runtime context, prompt structure, and injected state before model invocation. · `src/syrin/agent/_context_builder.py`
- [Tool Exec](https://github.com/syrin-labs/syrin-harness/blob/main/src/syrin/agent/_tool_exec.py): tool runtime · Resolves and executes tool calls during an agent run. · `src/syrin/agent/_tool_exec.py`
- [Model Layer](https://github.com/syrin-labs/syrin-harness/blob/main/src/syrin/agent/_model_provider.py): model/provider bridge · Resolves model abstractions and vendor providers for agent execution. · `src/syrin/agent/_model_provider.py`

### Safety & Control

Budget, guardrails, rate limits, and human approval gates.

- [Budget Control](https://github.com/syrin-labs/syrin-harness/blob/main/src/syrin/budget/_core.py): cost gate · Preflight estimation, live enforcement, pools, and forecast/history for runtime cost control. · `src/syrin/budget/_core.py`
- [Rate Limits](https://github.com/syrin-labs/syrin-harness/blob/main/src/syrin/ratelimit/_manager.py): traffic gate · Rate-window enforcement and retry behavior around calls. · `src/syrin/ratelimit/_manager.py`
- [Guardrails](https://github.com/syrin-labs/syrin-harness/blob/main/src/syrin/guardrails/engine.py): safety engine · Content, PII, authority, citation, fact, and prompt-injection checks. · `src/syrin/guardrails/engine.py`
- [HITL Gate](https://github.com/syrin-labs/syrin-harness/blob/main/src/syrin/hitl/_gate.py): approval gate · Human approval sessions and gating before continuing execution. · `src/syrin/hitl/_gate.py`

### State & Retrieval

Memory, knowledge, and persistence systems.

- [Memory](https://github.com/syrin-labs/syrin-harness/blob/main/src/syrin/memory/store.py): persistent memory · Conversational and long-term memory with pluggable backends. · `src/syrin/memory/store.py`
- [Knowledge RAG](https://github.com/syrin-labs/syrin-harness/blob/main/src/syrin/knowledge/_core.py): document retrieval · Loaders, chunkers, stores, grounding, and verification for knowledge-backed answers. · `src/syrin/knowledge/_core.py`

### Multi-Agent

Swarm, workflow, task, and routing coordination.

- [Swarm](https://github.com/syrin-labs/syrin-harness/blob/main/src/syrin/swarm/_core.py): agent coordination · Multi-agent coordination, authority, handoff, broadcast, and control-plane state. · `src/syrin/swarm/_core.py`
- [Workflow](https://github.com/syrin-labs/syrin-harness/blob/main/src/syrin/workflow/_core.py): step executor · Structured multi-step execution with lifecycle, branching, and visualization. · `src/syrin/workflow/_core.py`
- [Router](https://github.com/syrin-labs/syrin-harness/blob/main/src/syrin/router/router.py): model routing · Chooses models or providers based on capability, modality, or cost. · `src/syrin/router/router.py`

### Interfaces

Serving, remote control, MCP, and playground UI.

- [Serve](https://github.com/syrin-labs/syrin-harness/blob/main/src/syrin/serve/http.py): service boundary · CLI, HTTP, stdio, discovery, and playground serving for agents. · `src/syrin/serve/http.py`
- [Remote Config](https://github.com/syrin-labs/syrin-harness/blob/main/src/syrin/remote/_core.py): control plane · Remote negotiation, schema export, transport, and versioned configuration. · `src/syrin/remote/_core.py`
- [MCP](https://github.com/syrin-labs/syrin-harness/blob/main/src/syrin/mcp/server.py): interop endpoint · Model Context Protocol client/server and transport support. · `src/syrin/mcp/server.py`
- [Playground UI](https://github.com/syrin-labs/syrin-harness/blob/main/playground/app/page.tsx): Next.js ui · Browser-based debugging and agent inspection interface. · `playground/app/page.tsx`

### Observability

Logging, audit, hooks, tracing, and debug tooling.

- [Observability](https://github.com/syrin-labs/syrin-harness/blob/main/src/syrin/observability/_core.py): telemetry layer · Hooks, tracing, metrics, audit, logging, and replay surfaces. · `src/syrin/observability/_core.py`
- [Debug UI](https://github.com/syrin-labs/syrin-harness/blob/main/src/syrin/debug/_ui.py): inspection tools · Pry/debug panels, state export, and live run inspection. · `src/syrin/debug/_ui.py`

## Connections

- Agent API → Run Core: runs · Public API delegates into the centralized execution pipeline.
- Run Core → Context & Prompt: builds · Creates runtime context and prompt material before model invocation.
- Run Core → Model Layer: calls · Resolves a model/provider pair for the current run.
- Run Core → Tool Exec: executes · Runs tools and feeds results back into the agent loop.
- Run Core → Budget Control: checks · Enforces preflight and live budget constraints during execution.
- Run Core → Rate Limits: throttles · Applies request pacing and retry policy.
- Run Core → Guardrails: filters · Validates inputs, outputs, and intermediate decisions.
- Run Core → HITL Gate: escalates · Pauses for explicit human approval when required.
- Run Core → Memory: reads/writes · Stores and retrieves conversational and persistent memory.
- Run Core → Knowledge RAG: grounds · Pulls retrieved knowledge into the run for RAG and verification.
- Router → Model Layer: selects · Chooses the provider/model backing the run.
- Swarm → Run Core: orchestrates · Coordinates multiple agents through shared run mechanics.
- Workflow → Run Core: steps · Uses the agent runtime inside structured workflow steps.
- Swarm → Router: routes · Supports agent-to-agent and model routing decisions.
- Workflow → Budget Control: budgets · Propagates budget constraints across multi-step execution.
- Serve → Agent API: exposes · Wraps the agent API over HTTP, CLI, and stdio.
- Serve → Remote Config: negotiates · Loads remote configuration and service metadata.
- Serve → MCP: bridges · Offers interoperable MCP endpoints.
- Serve → Playground UI: ships · Serves the bundled playground UI as a front door.
- Observability → Run Core: instruments · Captures lifecycle events from agent execution.
- Observability → Swarm: traces · Records multi-agent actions and control-plane events.
- Debug UI → Observability: consumes · Builds inspection UI on top of the event and trace stream.
- Guardrails → Observability: emits · Guardrail decisions feed audit and trace systems.
- Memory → Knowledge RAG: complements · Distinct retrieval systems for conversational memory and document knowledge.
