# amoorawat/ai-agent architecture

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

- Interactive diagram: https://gitdiagram.com/amoorawat/ai-agent
- Repository: https://github.com/amoorawat/ai-agent

## Overview

This repository is a terminal-based AI coding assistant. The visible workflow accepts a prompt in the CLI, runs it through the agent, builds conversation context, requests a streamed completion from OpenRouter, and renders text in the terminal. A tool framework and a file-reading tool are present, but the sampled agent loop does not dispatch returned tool calls, so that is shown as a separate capability rather than an active workflow. Several links are based on source-index references; unsampled behavior is not assumed.

Main entry points: [`agent/agent.py`](https://github.com/amoorawat/ai-agent/blob/main/agent/agent.py) (Agent Loop), [`tools/builtin/read_file.py`](https://github.com/amoorawat/ai-agent/blob/main/tools/builtin/read_file.py) (File Reader), [`main.py`](https://github.com/amoorawat/ai-agent/blob/main/main.py) (CLI), [`context/manager.py`](https://github.com/amoorawat/ai-agent/blob/main/context/manager.py) (Conversation Context)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_interaction["CLI Interaction"]
  node_cli["CLI<br/>[main.py]"]
  node_terminal_ui["Terminal UI<br/>[tui.py]"]
end

subgraph group_agent_core["Agent Core"]
  node_agent["Agent Loop<br/>[agent.py]"]
  node_agent_events["Agent Events<br/>[events.py]"]
end

subgraph group_model_context["Model and Context"]
  node_context["Conversation Context<br/>[manager.py]"]
  node_system_prompt["System Prompt<br/>[system.py]"]
  node_text_utils["Token Utilities<br/>[text.py]"]
  node_llm_client["LLM Client<br/>[llm_client.py]"]
  node_response_types["Stream Types<br/>[response.py]"]
end

subgraph group_tooling["Tooling"]
  node_registry["Tool Registry<br/>[registry.py]"]
  node_tool_contract["Tool Contract<br/>[base.py]"]
  node_builtin_tools["Built-in Tools<br/>[__init__.py]"]
  node_read_file["File Reader<br/>[read_file.py]"]
  node_path_utils["Path Utilities<br/>[paths.py]"]
end

node_user(("User"))
node_openrouter{{"OpenRouter API"}}
node_workspace[("Workspace Files")]

node_user -->|"enters prompt"| node_cli
node_cli -->|"runs agent"| node_agent
node_agent -->|"emits events"| node_agent_events
node_agent -->|"updates context"| node_context
node_agent -->|"gets schemas"| node_registry
node_agent -->|"requests completion"| node_llm_client
node_llm_client -->|"sends chat request"| node_openrouter
node_llm_client -->|"builds stream events"| node_response_types
node_agent_events -->|"uses token usage"| node_response_types
node_context -->|"loads prompt"| node_system_prompt
node_context -->|"counts tokens"| node_text_utils
node_cli -->|"renders stream"| node_terminal_ui
node_registry -->|"loads defaults"| node_builtin_tools
node_registry -->|"registers and invokes"| node_tool_contract
node_builtin_tools -->|"provides tool"| node_read_file
node_read_file -->|"implements contract"| node_tool_contract
node_read_file -->|"resolves and checks paths"| node_path_utils
node_read_file -->|"counts and truncates"| node_text_utils
node_read_file -->|"reads files"| node_workspace

click node_cli "https://github.com/amoorawat/ai-agent/blob/main/main.py"
click node_terminal_ui "https://github.com/amoorawat/ai-agent/blob/main/ui/tui.py"
click node_agent "https://github.com/amoorawat/ai-agent/blob/main/agent/agent.py"
click node_agent_events "https://github.com/amoorawat/ai-agent/blob/main/agent/events.py"
click node_context "https://github.com/amoorawat/ai-agent/blob/main/context/manager.py"
click node_system_prompt "https://github.com/amoorawat/ai-agent/blob/main/prompts/system.py"
click node_text_utils "https://github.com/amoorawat/ai-agent/blob/main/utils/text.py"
click node_llm_client "https://github.com/amoorawat/ai-agent/blob/main/client/llm_client.py"
click node_response_types "https://github.com/amoorawat/ai-agent/blob/main/client/response.py"
click node_registry "https://github.com/amoorawat/ai-agent/blob/main/tools/registry.py"
click node_tool_contract "https://github.com/amoorawat/ai-agent/blob/main/tools/base.py"
click node_builtin_tools "https://github.com/amoorawat/ai-agent/blob/main/tools/builtin/__init__.py"
click node_read_file "https://github.com/amoorawat/ai-agent/blob/main/tools/builtin/read_file.py"
click node_path_utils "https://github.com/amoorawat/ai-agent/blob/main/utils/paths.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_cli,node_terminal_ui,node_user toneBlue
class node_agent,node_agent_events,node_workspace toneAmber
class node_context,node_system_prompt,node_text_utils,node_llm_client,node_response_types,node_openrouter toneMint
class node_registry,node_tool_contract,node_builtin_tools,node_read_file,node_path_utils toneRose
```

## Components

### CLI Interaction

- [CLI](https://github.com/amoorawat/ai-agent/blob/main/main.py): `main.py`
- [Terminal UI](https://github.com/amoorawat/ai-agent/blob/main/ui/tui.py): `ui/tui.py`

### Agent Core

- [Agent Loop](https://github.com/amoorawat/ai-agent/blob/main/agent/agent.py): `agent/agent.py`
- [Agent Events](https://github.com/amoorawat/ai-agent/blob/main/agent/events.py): `agent/events.py`

### Model and Context

- [Conversation Context](https://github.com/amoorawat/ai-agent/blob/main/context/manager.py): `context/manager.py`
- [System Prompt](https://github.com/amoorawat/ai-agent/blob/main/prompts/system.py): `prompts/system.py`
- [Token Utilities](https://github.com/amoorawat/ai-agent/blob/main/utils/text.py): `utils/text.py`
- [LLM Client](https://github.com/amoorawat/ai-agent/blob/main/client/llm_client.py): `client/llm_client.py`
- [Stream Types](https://github.com/amoorawat/ai-agent/blob/main/client/response.py): `client/response.py`

### Tooling

- [Tool Registry](https://github.com/amoorawat/ai-agent/blob/main/tools/registry.py): `tools/registry.py`
- [Tool Contract](https://github.com/amoorawat/ai-agent/blob/main/tools/base.py): `tools/base.py`
- [Built-in Tools](https://github.com/amoorawat/ai-agent/blob/main/tools/builtin/__init__.py): `tools/builtin/__init__.py`
- [File Reader](https://github.com/amoorawat/ai-agent/blob/main/tools/builtin/read_file.py): `tools/builtin/read_file.py`
- [Path Utilities](https://github.com/amoorawat/ai-agent/blob/main/utils/paths.py): `utils/paths.py`

### Other components

- **User**
- **OpenRouter API**
- **Workspace Files**

## Connections

- User → CLI: enters prompt (evidence: `main.py`)
- CLI → Agent Loop: runs agent (evidence: `main.py`)
- Agent Loop → Agent Events: emits events (evidence: `agent/agent.py`)
- Agent Loop → Conversation Context: updates context (evidence: `agent/agent.py`)
- Agent Loop → Tool Registry: gets schemas (evidence: `agent/agent.py`)
- Agent Loop → LLM Client: requests completion (evidence: `agent/agent.py`)
- LLM Client → OpenRouter API: sends chat request (evidence: `client/llm_client.py`)
- LLM Client → Stream Types: builds stream events (evidence: `client/llm_client.py`)
- Agent Events → Stream Types: uses token usage (evidence: `agent/events.py`)
- Conversation Context → System Prompt: loads prompt (evidence: `context/manager.py`)
- Conversation Context → Token Utilities: counts tokens (evidence: `context/manager.py`)
- CLI → Terminal UI: renders stream (evidence: `main.py`)
- Tool Registry → Built-in Tools: loads defaults (evidence: `tools/registry.py`)
- Tool Registry → Tool Contract: registers and invokes (evidence: `tools/registry.py`)
- Built-in Tools → File Reader: provides tool (evidence: `tools/builtin/__init__.py`)
- File Reader → Tool Contract: implements contract (evidence: `tools/builtin/read_file.py`)
- File Reader → Path Utilities: resolves and checks paths (evidence: `tools/builtin/read_file.py`)
- File Reader → Token Utilities: counts and truncates (evidence: `tools/builtin/read_file.py`)
- File Reader → Workspace Files: reads files (evidence: `tools/builtin/read_file.py`)
