# go-kratos/blades architecture

> Architecture diagram of the GitHub repository go-kratos/blades, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/go-kratos/blades
- Repository: https://github.com/go-kratos/blades

## Overview

#### Purpose and entry points
Blades is a Go framework for building multimodal LLM agents: model invocation, tool calling, memory, middleware, streaming, and composable workflows. Library consumers construct agents through the public package rooted at `agent.go`, then execute them via `runner.go`. The standalone “blades” CLI has a separate module and entry point at `cmd/blades/main.go`; documentation generation is another executable at `cmd/docs/main.go`.

#### Core agent execution
The primary runtime abstraction is `Agent`, implemented by the framework agent and exercised through a `Runner`. An invocation enters the agent with user messages/instructions, builds model context, invokes the configured model (potentially streaming), processes tool interactions, and yields messages or errors. Core contracts and orchestration are concentrated in:
- `agent.go`
- `runner.go`
- `core.go`

#### Model and multimodal boundary
`ModelProvider` isolates framework request/message types from vendor-specific APIs. The core model contract lives in `model.go`; adapters translate it for OpenAI, Anthropic/Claude, and Gemini, including image/audio capabilities where supported. This is the primary external-service boundary, driven by provider credentials and endpoints supplied by callers.
- `model.go`
- `contrib/openai/chat.go`
- `contrib/anthropic/claude.go`

#### Tools, skills, and MCP integration
Tools expose schemas plus handlers so a model can request external actions; tool middleware and resolution provide a controlled execution boundary. Skills are loaded from directories or embedded files and injected into agent prompting/toolsets. MCP support adds client, transport, configuration, and endpoint resolution for external tool servers.
- `tools/tool.go`
- `skills/loader.go`
- `contrib/mcp/client.go`

#### Context, sessions, and memory
Conversation state is represented by messages, invocations, and sessions. Agents can persist/search memory through a pluggable memory interface; the bundled implementation is in-memory. Context strategies manage prompt size by windowing or summarizing accumulated history, forming the key boundary between unbounded conversation state and finite model context.
- `session.go`
- `memory/memory.go`
- `context/summary/summary.go`

#### Middleware and execution controls
Runner and tool middleware wrap execution for cross-cutting behavior such as confirmation and retries. Streaming uses a generator abstraction, allowing callers and channels to consume incremental model output. Evaluation support provides criteria-based assessment of agent results.
- `middleware.go`
- `middleware/retry.go`
- `stream/stream.go`

#### Workflow composition
The graph subsystem executes task graphs with state, retries, middleware, and checkpoints; the higher-level flow package supplies sequential, parallel, routing, loop, and deep-agent compositions. Recipes declaratively load, validate, template, and build agent/workflow definitions from YAML-like specifications.
- `graph/executor.go`
- `flow/sequential.go`
- `recipe/loader.go`

#### CLI application and delivery channels
The separate `cmd/blades` application bootstraps workspaces, configures model registries and runtime services, and runs chat, daemon, cron, memory, and operational commands. It supports local CLI interaction plus Lark and Weixin channel adapters; cron enables scheduled/heartbeat execution. Workspace templates establish agent identity, memory, tool, and instruction files.
- `cmd/blades/cmd/root.go`
- `cmd/blades/internal/app/runner.go`
- `cmd/blades/internal/channel/lark/channel.go`

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_framework["Framework Runtime"]
  node_public_agent["Public Agent API<br/>Go package<br/>[agent.go]"]
  node_runner["Runner<br/>execution runtime<br/>[runner.go]"]
  node_core["Agent Core<br/>orchestration<br/>[core.go]"]
  node_model_contract{{"ModelProvider<br/>provider contract<br/>[model.go]"}}
  node_sessions[("Sessions &amp; Messages<br/>conversation state<br/>[session.go]")]
  node_memory[("Memory Interface<br/>pluggable storage<br/>[memory.go]")]
  node_context_summary["Context Strategy<br/>history compaction<br/>[summary.go]"]
  node_middleware["Execution Middleware<br/>cross-cutting controls<br/>[middleware.go]"]
  node_retry["Retry Middleware<br/>execution control<br/>[retry.go]"]
  node_stream["Streaming<br/>generator abstraction<br/>[stream.go]"]
end

subgraph group_integration["Provider &amp; Tool Integrations"]
  node_tools{{"Tools<br/>tool contract<br/>[tool.go]"}}
  node_skills["Skills Loader<br/>prompt/toolset source<br/>[loader.go]"]
  node_mcp{{"MCP Client<br/>external tool gateway<br/>[client.go]"}}
  node_openai{{"OpenAI Adapter<br/>model adapter<br/>[chat.go]"}}
  node_anthropic{{"Anthropic Adapter<br/>model adapter<br/>[claude.go]"}}
end

subgraph group_composition["Composition &amp; Definitions"]
  node_graph_executor["Graph Executor<br/>workflow runtime<br/>[executor.go]"]
  node_flow["Flow Composition<br/>workflow builder<br/>[sequential.go]"]
  node_recipe["Recipe Loader<br/>declarative builder<br/>[loader.go]"]
end

subgraph group_cli["Blades CLI"]
  node_cli_main["Blades CLI Entry<br/>CLI executable<br/>[main.go]"]
  node_cli_root["CLI Commands<br/>command surface<br/>[root.go]"]
  node_cli_app_runner["CLI App Runner<br/>application bootstrap<br/>[runner.go]"]
  node_lark{{"Lark Channel<br/>channel adapter<br/>[channel.go]"}}
end

node_public_agent -->|"executed by"| node_runner
node_runner -->|"orchestrates"| node_core
node_runner -->|"wrapped by"| node_middleware
node_middleware -->|"includes"| node_retry
node_core -->|"reads and updates"| node_sessions
node_core -->|"persists/searches"| node_memory
node_core -->|"compacts history with"| node_context_summary
node_core -->|"invokes"| node_model_contract
node_core -->|"resolves tool calls"| node_tools
node_core -->|"injects"| node_skills
node_runner -->|"emits incremental output"| node_stream
node_model_contract -->|"implemented by"| node_openai
node_model_contract -->|"implemented by"| node_anthropic
node_tools -->|"extends via"| node_mcp
node_flow -->|"executes through"| node_graph_executor
node_recipe -->|"builds"| node_public_agent
node_recipe -->|"builds"| node_flow
node_cli_main -->|"starts"| node_cli_root
node_cli_root -->|"dispatches to"| node_cli_app_runner
node_cli_app_runner -->|"runs agents with"| node_runner
node_cli_app_runner -->|"connects channel"| node_lark

click node_public_agent "https://github.com/go-kratos/blades/blob/main/agent.go"
click node_runner "https://github.com/go-kratos/blades/blob/main/runner.go"
click node_core "https://github.com/go-kratos/blades/blob/main/core.go"
click node_model_contract "https://github.com/go-kratos/blades/blob/main/model.go"
click node_sessions "https://github.com/go-kratos/blades/blob/main/session.go"
click node_memory "https://github.com/go-kratos/blades/blob/main/memory/memory.go"
click node_context_summary "https://github.com/go-kratos/blades/blob/main/context/summary/summary.go"
click node_middleware "https://github.com/go-kratos/blades/blob/main/middleware.go"
click node_retry "https://github.com/go-kratos/blades/blob/main/middleware/retry.go"
click node_stream "https://github.com/go-kratos/blades/blob/main/stream/stream.go"
click node_tools "https://github.com/go-kratos/blades/blob/main/tools/tool.go"
click node_skills "https://github.com/go-kratos/blades/blob/main/skills/loader.go"
click node_mcp "https://github.com/go-kratos/blades/blob/main/contrib/mcp/client.go"
click node_openai "https://github.com/go-kratos/blades/blob/main/contrib/openai/chat.go"
click node_anthropic "https://github.com/go-kratos/blades/blob/main/contrib/anthropic/claude.go"
click node_graph_executor "https://github.com/go-kratos/blades/blob/main/graph/executor.go"
click node_flow "https://github.com/go-kratos/blades/blob/main/flow/sequential.go"
click node_recipe "https://github.com/go-kratos/blades/blob/main/recipe/loader.go"
click node_cli_main "https://github.com/go-kratos/blades/blob/main/cmd/blades/main.go"
click node_cli_root "https://github.com/go-kratos/blades/blob/main/cmd/blades/cmd/root.go"
click node_cli_app_runner "https://github.com/go-kratos/blades/blob/main/cmd/blades/internal/app/runner.go"
click node_lark "https://github.com/go-kratos/blades/blob/main/cmd/blades/internal/channel/lark/channel.go"

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_public_agent,node_runner,node_core,node_model_contract,node_sessions,node_memory,node_context_summary,node_middleware,node_retry,node_stream toneBlue
class node_tools,node_skills,node_mcp,node_openai,node_anthropic toneAmber
class node_graph_executor,node_flow,node_recipe toneMint
class node_cli_main,node_cli_root,node_cli_app_runner,node_lark toneRose
```

## Components

### Framework Runtime

- [Public Agent API](https://github.com/go-kratos/blades/blob/main/agent.go): Go package · Consumer entry point for constructing agents. · `agent.go`
- [Runner](https://github.com/go-kratos/blades/blob/main/runner.go): execution runtime · Executes agent invocations and returns results or streams. · `runner.go`
- [Agent Core](https://github.com/go-kratos/blades/blob/main/core.go): orchestration · Builds context, invokes models, and coordinates tool turns. · `core.go`
- [ModelProvider](https://github.com/go-kratos/blades/blob/main/model.go): provider contract · Vendor-neutral model and multimodal request boundary. · `model.go`
- [Sessions & Messages](https://github.com/go-kratos/blades/blob/main/session.go): conversation state · `session.go`
- [Memory Interface](https://github.com/go-kratos/blades/blob/main/memory/memory.go): pluggable storage · Persists and searches agent memory. · `memory/memory.go`
- [Context Strategy](https://github.com/go-kratos/blades/blob/main/context/summary/summary.go): history compaction · Summarizes accumulated history to fit model context. · `context/summary/summary.go`
- [Execution Middleware](https://github.com/go-kratos/blades/blob/main/middleware.go): cross-cutting controls · Wraps runner and tool execution. · `middleware.go`
- [Retry Middleware](https://github.com/go-kratos/blades/blob/main/middleware/retry.go): execution control · `middleware/retry.go`
- [Streaming](https://github.com/go-kratos/blades/blob/main/stream/stream.go): generator abstraction · Delivers incremental model output to callers or channels. · `stream/stream.go`

### Provider & Tool Integrations

- [Tools](https://github.com/go-kratos/blades/blob/main/tools/tool.go): tool contract · Schemas and handlers for model-requested actions. · `tools/tool.go`
- [Skills Loader](https://github.com/go-kratos/blades/blob/main/skills/loader.go): prompt/toolset source · Loads directory or embedded skills into agents. · `skills/loader.go`
- [MCP Client](https://github.com/go-kratos/blades/blob/main/contrib/mcp/client.go): external tool gateway · Resolves and connects to MCP tool servers. · `contrib/mcp/client.go`
- [OpenAI Adapter](https://github.com/go-kratos/blades/blob/main/contrib/openai/chat.go): model adapter · `contrib/openai/chat.go`
- [Anthropic Adapter](https://github.com/go-kratos/blades/blob/main/contrib/anthropic/claude.go): model adapter · `contrib/anthropic/claude.go`

### Composition & Definitions

- [Graph Executor](https://github.com/go-kratos/blades/blob/main/graph/executor.go): workflow runtime · Runs task graphs with state, retries, middleware, and checkpoints. · `graph/executor.go`
- [Flow Composition](https://github.com/go-kratos/blades/blob/main/flow/sequential.go): workflow builder · Provides sequential, parallel, routing, loop, and deep-agent flows. · `flow/sequential.go`
- [Recipe Loader](https://github.com/go-kratos/blades/blob/main/recipe/loader.go): declarative builder · Loads and builds agent or workflow definitions. · `recipe/loader.go`

### Blades CLI

- [Blades CLI Entry](https://github.com/go-kratos/blades/blob/main/cmd/blades/main.go): CLI executable · Separate module entry point. · `cmd/blades/main.go`
- [CLI Commands](https://github.com/go-kratos/blades/blob/main/cmd/blades/cmd/root.go): command surface · Chat, daemon, cron, memory, and operational commands. · `cmd/blades/cmd/root.go`
- [CLI App Runner](https://github.com/go-kratos/blades/blob/main/cmd/blades/internal/app/runner.go): application bootstrap · Configures workspace, model registries, and runtime services. · `cmd/blades/internal/app/runner.go`
- [Lark Channel](https://github.com/go-kratos/blades/blob/main/cmd/blades/internal/channel/lark/channel.go): channel adapter · `cmd/blades/internal/channel/lark/channel.go`

## Connections

- Public Agent API → Runner: executed by
- Runner → Agent Core: orchestrates
- Runner → Execution Middleware: wrapped by
- Execution Middleware → Retry Middleware: includes
- Agent Core → Sessions & Messages: reads and updates
- Agent Core → Memory Interface: persists/searches
- Agent Core → Context Strategy: compacts history with
- Agent Core → ModelProvider: invokes
- Agent Core → Tools: resolves tool calls
- Agent Core → Skills Loader: injects
- Runner → Streaming: emits incremental output
- ModelProvider → OpenAI Adapter: implemented by
- ModelProvider → Anthropic Adapter: implemented by
- Tools → MCP Client: extends via
- Flow Composition → Graph Executor: executes through
- Recipe Loader → Public Agent API: builds
- Recipe Loader → Flow Composition: builds
- Blades CLI Entry → CLI Commands: starts
- CLI Commands → CLI App Runner: dispatches to
- CLI App Runner → Runner: runs agents with
- CLI App Runner → Lark Channel: connects channel
