# sentdex/minion architecture

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

- Interactive diagram: https://gitdiagram.com/sentdex/minion
- Repository: https://github.com/sentdex/minion

## Overview

#### Purpose and Entry Points
Minion is a single-process Python CLI coding agent optimized for minimal LLM context overhead. Its implementation is intentionally centralized in `minion.py`; it starts an interactive terminal REPL, or supports session-listing/resume CLI modes. Packaging exposes the `minion` command through `setup.py` and project metadata in `pyproject.toml`.

Paths: `minion.py`, `setup.py`, `pyproject.toml`

#### Runtime and Backend Boundary
The process loads environment-based source configuration, including `~/.env`, then creates an OpenAI SDK client for an OpenAI-compatible endpoint. Sources may be local llama.cpp, vLLM, SGLang, remote OpenAI-compatible APIs, Together, OpenRouter, or LiteLLM proxies in front of Bedrock. Source switching preserves conversation history while changing endpoint/model behavior.

Paths: `minion.py`, `sources.example.env`, `requirements.txt`

#### REPL and Conversation Orchestration
The main control loop combines a raw-terminal multiline editor (with non-TTY fallback), slash-command dispatch, status output, prompt history, and streamed model turns. It maintains the model-visible message array and drives iterative tool-call turns until the model produces a user-visible final response or recovery returns control to the user.

Paths: `minion.py`

#### Model Streaming, Tool Protocol, and Recovery
Streaming responses are consumed directly from the OpenAI API, including optional `reasoning_content`. Native function calls are preferred; unsupported servers use parsed standalone `[minion_tool_call]` text blocks. Tool-result delimiters are escaped before re-entering context. Guards recover from malformed SSE/tool arguments, empty turns, repetition failures, and reasoning-only stalls through bounded retries and forced-final-answer requests.

Paths: `minion.py`, `tests/test_text_tool_protocol.py`, `tests/test_empty_turn_recovery.py`

#### Workspace Tool Execution and Approval Boundary
The agent exposes a deliberately small local capability surface: file reads, writes, exact-match edits, directory listings, and shell commands. State-changing operations and shell execution pass through a separate cheap-model risk classification call, then an interactive approval gate unless the active approval threshold or YOLO mode permits execution. Execution results are capped/deduplicated before being appended to LLM history, limiting context growth.

Paths: `minion.py`, `tests/test_approval_env.py`, `tests/test_risk_context.py`

#### Context Management
Minion tracks estimated/current context usage against a per-source/model maximum. It probes and caches context limits using local-server metadata where available or an over-limit chat request for remote endpoints. Manual compression and automatic threshold-based compression summarize older conversation history while retaining recent turns verbatim, preserving continued task context within constrained local-model windows.

Paths: `minion.py`, `tests/test_context_window.py`, `tests/test_autocompress.py`

#### Persistence, Observability, and External Reporting
Each conversation is auto-saved as human-readable session JSON, including messages, source/cwd metadata, descriptions, and cumulative normalized token usage. Resume restores both transcript and originating source. Requests/SSE chunks are logged locally to `llamacpp.log`; an optional metrics URL receives fire-and-forget cumulative usage posts and disables after failure.

Paths: `minion.py`, `tests/test_sessions.py`

Main entry points: [`minion.py`](https://github.com/sentdex/minion/blob/master/minion.py) (Turn orchestrator), [`setup.py`](https://github.com/sentdex/minion/blob/master/setup.py) (Package metadata), [`pyproject.toml`](https://github.com/sentdex/minion/blob/master/pyproject.toml) (Project metadata), [`sources.example.env`](https://github.com/sentdex/minion/blob/master/sources.example.env) (Source env template)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["CLI Entry"]
  node_package["Package metadata<br/>Python packaging<br/>[setup.py]"]
  node_project["Project metadata<br/>Python project<br/>[pyproject.toml]"]
  node_cli["CLI modes<br/>command entry<br/>[minion.py]"]
  node_source_example["Source env template<br/>configuration example"]
  node_dependencies["Runtime dependencies<br/>Python requirements<br/>[requirements.txt]"]
end

subgraph group_runtime["Agent Runtime"]
  node_config["Source configuration<br/>environment config<br/>[minion.py]"]
  node_repl["Interactive REPL<br/>terminal loop<br/>[minion.py]"]
  node_orchestrator["Turn orchestrator<br/>conversation controller<br/>[minion.py]"]
end

subgraph group_model["Model and Context"]
  node_client{{"OpenAI-compatible client<br/>LLM backend adapter<br/>[minion.py]"}}
  node_streaming["Streaming and recovery<br/>response processor<br/>[minion.py]"]
  node_tool_protocol["Tool protocol<br/>function-call fallback<br/>[minion.py]"]
  node_context["Context manager<br/>window management<br/>[minion.py]"]
  node_limits["Context-limit probe<br/>capability discovery<br/>[minion.py]"]
end

subgraph group_workspace["Workspace Safety"]
  node_tools["Workspace tools<br/>local capability surface<br/>[minion.py]"]
  node_risk["Risk classifier<br/>cheap-model check<br/>[minion.py]"]
  node_approval{{"Approval gate<br/>interactive safety boundary<br/>[minion.py]"}}
  node_result_control["Tool result control<br/>context limiter<br/>[minion.py]"]
end

subgraph group_state["Persistence and Reporting"]
  node_sessions[("Session store<br/>JSON persistence<br/>[minion.py]")]
  node_logging["Local request log<br/>SSE observability<br/>[minion.py]"]
  node_metrics{{"Usage reporter<br/>optional metrics sink<br/>[minion.py]"}}
end

node_package -->|"exposes command"| node_cli
node_project -->|"defines project"| node_package
node_source_example -.->|"documents settings"| node_config
node_dependencies -.->|"provides SDK"| node_client
node_cli -->|"starts"| node_repl
node_cli -->|"lists or resumes"| node_sessions
node_config -->|"configures endpoint"| node_client
node_repl -->|"submits turns"| node_orchestrator
node_orchestrator -->|"requests model turn"| node_streaming
node_streaming -->|"streams API calls"| node_client
node_streaming -->|"interprets calls"| node_tool_protocol
node_tool_protocol -->|"invokes"| node_tools
node_tools -->|"checks risky actions"| node_risk
node_risk -->|"requires decision"| node_approval
node_approval -->|"permits execution"| node_tools
node_tools -->|"returns output"| node_result_control
node_result_control -->|"adds tool result"| node_orchestrator
node_orchestrator -->|"monitors history"| node_context
node_context -->|"uses limit"| node_limits
node_limits -->|"probes backend"| node_client
node_orchestrator -->|"autosaves"| node_sessions
node_streaming -.->|"logs chunks"| node_logging
node_sessions -.->|"reports usage"| node_metrics

click node_package "https://github.com/sentdex/minion/blob/master/setup.py"
click node_project "https://github.com/sentdex/minion/blob/master/pyproject.toml"
click node_cli "https://github.com/sentdex/minion/blob/master/minion.py"
click node_config "https://github.com/sentdex/minion/blob/master/minion.py"
click node_repl "https://github.com/sentdex/minion/blob/master/minion.py"
click node_orchestrator "https://github.com/sentdex/minion/blob/master/minion.py"
click node_client "https://github.com/sentdex/minion/blob/master/minion.py"
click node_streaming "https://github.com/sentdex/minion/blob/master/minion.py"
click node_tool_protocol "https://github.com/sentdex/minion/blob/master/minion.py"
click node_context "https://github.com/sentdex/minion/blob/master/minion.py"
click node_limits "https://github.com/sentdex/minion/blob/master/minion.py"
click node_tools "https://github.com/sentdex/minion/blob/master/minion.py"
click node_risk "https://github.com/sentdex/minion/blob/master/minion.py"
click node_approval "https://github.com/sentdex/minion/blob/master/minion.py"
click node_result_control "https://github.com/sentdex/minion/blob/master/minion.py"
click node_sessions "https://github.com/sentdex/minion/blob/master/minion.py"
click node_logging "https://github.com/sentdex/minion/blob/master/minion.py"
click node_metrics "https://github.com/sentdex/minion/blob/master/minion.py"
click node_source_example "https://github.com/sentdex/minion/blob/master/sources.example.env"
click node_dependencies "https://github.com/sentdex/minion/blob/master/requirements.txt"

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_package,node_project,node_cli,node_source_example,node_dependencies toneBlue
class node_config,node_repl,node_orchestrator toneAmber
class node_client,node_streaming,node_tool_protocol,node_context,node_limits toneMint
class node_tools,node_risk,node_approval,node_result_control toneRose
class node_sessions,node_logging,node_metrics toneIndigo
```

## Components

### CLI Entry

- [Package metadata](https://github.com/sentdex/minion/blob/master/setup.py): Python packaging · Installs the minion console command. · `setup.py`
- [Project metadata](https://github.com/sentdex/minion/blob/master/pyproject.toml): Python project · `pyproject.toml`
- [CLI modes](https://github.com/sentdex/minion/blob/master/minion.py): command entry · Starts REPL or lists and resumes sessions. · `minion.py`
- [Source env template](https://github.com/sentdex/minion/blob/master/sources.example.env): configuration example · `sources.example.env`
- [Runtime dependencies](https://github.com/sentdex/minion/blob/master/requirements.txt): Python requirements · `requirements.txt`

### Agent Runtime

- [Source configuration](https://github.com/sentdex/minion/blob/master/minion.py): environment config · Loads endpoint and model settings, including ~/.env. · `minion.py`
- [Interactive REPL](https://github.com/sentdex/minion/blob/master/minion.py): terminal loop · Handles multiline input, slash commands, history, and status. · `minion.py`
- [Turn orchestrator](https://github.com/sentdex/minion/blob/master/minion.py): conversation controller · Maintains messages and iterates model and tool turns. · `minion.py`

### Model and Context

- [OpenAI-compatible client](https://github.com/sentdex/minion/blob/master/minion.py): LLM backend adapter · Switches sources while preserving conversation history. · `minion.py`
- [Streaming and recovery](https://github.com/sentdex/minion/blob/master/minion.py): response processor · Consumes SSE, parses reasoning/tool calls, and applies bounded recovery. · `minion.py`
- [Tool protocol](https://github.com/sentdex/minion/blob/master/minion.py): function-call fallback · Uses native calls or parsed [minion_tool_call] blocks. · `minion.py`
- [Context manager](https://github.com/sentdex/minion/blob/master/minion.py): window management · Tracks limits and compresses older history when needed. · `minion.py`
- [Context-limit probe](https://github.com/sentdex/minion/blob/master/minion.py): capability discovery · Caches local metadata or remote over-limit probe results. · `minion.py`

### Workspace Safety

- [Workspace tools](https://github.com/sentdex/minion/blob/master/minion.py): local capability surface · Reads, writes, edits, lists directories, and runs shell commands. · `minion.py`
- [Risk classifier](https://github.com/sentdex/minion/blob/master/minion.py): cheap-model check · Classifies state-changing and shell operations. · `minion.py`
- [Approval gate](https://github.com/sentdex/minion/blob/master/minion.py): interactive safety boundary · Applies threshold or YOLO policy before execution. · `minion.py`
- [Tool result control](https://github.com/sentdex/minion/blob/master/minion.py): context limiter · Caps and deduplicates execution output before history insertion. · `minion.py`

### Persistence and Reporting

- [Session store](https://github.com/sentdex/minion/blob/master/minion.py): JSON persistence · Autosaves transcripts, metadata, and normalized usage. · `minion.py`
- [Local request log](https://github.com/sentdex/minion/blob/master/minion.py): SSE observability · Records requests and stream chunks in llamacpp.log. · `minion.py`
- [Usage reporter](https://github.com/sentdex/minion/blob/master/minion.py): optional metrics sink · Posts cumulative usage and disables after failure. · `minion.py`

## Connections

- Package metadata → CLI modes: exposes command
- Project metadata → Package metadata: defines project
- Source env template → Source configuration: documents settings
- Runtime dependencies → OpenAI-compatible client: provides SDK
- CLI modes → Interactive REPL: starts
- CLI modes → Session store: lists or resumes
- Source configuration → OpenAI-compatible client: configures endpoint
- Interactive REPL → Turn orchestrator: submits turns
- Turn orchestrator → Streaming and recovery: requests model turn
- Streaming and recovery → OpenAI-compatible client: streams API calls
- Streaming and recovery → Tool protocol: interprets calls
- Tool protocol → Workspace tools: invokes
- Workspace tools → Risk classifier: checks risky actions
- Risk classifier → Approval gate: requires decision
- Approval gate → Workspace tools: permits execution
- Workspace tools → Tool result control: returns output
- Tool result control → Turn orchestrator: adds tool result
- Turn orchestrator → Context manager: monitors history
- Context manager → Context-limit probe: uses limit
- Context-limit probe → OpenAI-compatible client: probes backend
- Turn orchestrator → Session store: autosaves
- Streaming and recovery → Local request log: logs chunks
- Session store → Usage reporter: reports usage
