# harshbhanushali26/ai-agent-engine architecture

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

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

## Overview

AI Agent Engine is a cost-conscious conversational agent that routes queries through cache, deterministic pattern matching, personal-context retrieval, and—when needed—a defensive LLM workflow. The main entry point coordinates cache and agent requests; the agent plans, validates, executes tools, recovers from failures, and generates a response. Other documented capabilities include web search, weather, text extraction, and usage quotas. Edges reflect the README, excerpts, and source-index references; several unsampled subsystems are represented at their documented directory paths.

Main entry points: [`core/agent.py`](https://github.com/harshbhanushali26/ai-agent-engine/blob/main/core/agent.py) (Agent orchestrator), [`tools/registry.py`](https://github.com/harshbhanushali26/ai-agent-engine/blob/main/tools/registry.py) (Tool registry), [`main.py`](https://github.com/harshbhanushali26/ai-agent-engine/blob/main/main.py) (Interactive CLI), [`core/routing/router.py`](https://github.com/harshbhanushali26/ai-agent-engine/blob/main/core/routing/router.py) (Pattern router)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["Query entry"]
  node_cli["Interactive CLI<br/>[main.py]"]
  node_cache[("Query cache<br/>[memory.py]")]
end

subgraph group_routing["Fast paths"]
  node_router["Pattern router<br/>[router.py]"]
  node_date_match["Date matcher"]
  node_math_match["Math matcher<br/>[math_pattern.py]"]
  node_text_match["Text matcher<br/>[text_pattern.py]"]
end

subgraph group_reasoning["Agent workflow"]
  node_agent["Agent orchestrator<br/>[agent.py]"]
  node_planner["Plan generation<br/>[planner.py]"]
  node_validator["Plan validation"]
  node_executor["Step execution<br/>[executor.py]"]
  node_runner["Tool runner<br/>[runner.py]"]
  node_failure["Failure classifier"]
  node_replanner["Plan recovery<br/>[replanner.py]"]
  node_state["Execution state<br/>[state.py]"]
  node_responder["Response generation<br/>[responder.py]"]
end

subgraph group_tools["Tool capabilities"]
  node_registry["Tool registry<br/>[registry.py]"]
  node_calculator["Safe calculator<br/>[calculate.py]"]
  node_datetime["Date-time tools<br/>[datetime.py]"]
  node_text_tools["Text operations<br/>[text_transform.py]"]
  node_web["Search and weather"]
  node_extraction["Text extraction<br/>[extract_text.py]"]
  node_rag[("Preference retrieval<br/>[knowledge_base.py]")]
end

subgraph group_operations["Usage and contracts"]
  node_usage[("Quota and cost tracking<br/>[usage_tracker.py]")]
  node_schemas["Tool contracts<br/>[schemas.py]"]
  node_tool_response["Tool response format<br/>[responses.py]"]
end

node_user(("User"))
node_llm{{"LLM service"}}

node_user -->|"submits query"| node_cli
node_cli -->|"checks cache"| node_cache
node_cli -->|"routes query"| node_router
node_cli -->|"sends cache miss"| node_agent
node_cli -->|"checks quota"| node_usage
node_router -->|"checks dates"| node_date_match
node_router -->|"checks math"| node_math_match
node_router -->|"checks text"| node_text_match
node_date_match -->|"uses operations"| node_datetime
node_math_match -->|"evaluates expression"| node_calculator
node_text_match -->|"uses operations"| node_text_tools
node_agent -->|"requests plan"| node_planner
node_agent -->|"validates plan"| node_validator
node_agent -->|"executes plan"| node_executor
node_agent -->|"generates response"| node_responder
node_agent -->|"classifies failure"| node_failure
node_agent -->|"repairs plan"| node_replanner
node_executor -->|"runs steps"| node_runner
node_executor -->|"resolves dependencies"| node_state
node_runner -->|"dispatches tools"| node_registry
node_replanner -->|"requests replacement plan"| node_planner
node_replanner -->|"validates replacement"| node_validator
node_planner -.->|"requests completion"| node_llm
node_responder -.->|"requests completion"| node_llm
node_registry -->|"registers handler"| node_calculator
node_registry -->|"registers handlers"| node_datetime
node_registry -->|"registers handler"| node_text_tools
node_registry -->|"registers handlers"| node_web
node_registry -->|"registers handler"| node_extraction
node_registry -->|"registers handler"| node_rag
node_cli -.->|"retrieves context"| node_rag
node_agent -->|"tracks calls"| node_usage
node_planner -->|"uses contracts"| node_schemas
node_validator -->|"checks tool catalog"| node_registry
node_calculator -->|"formats result"| node_tool_response
node_datetime -->|"formats result"| node_tool_response
node_responder -->|"uses contracts"| node_schemas

click node_cli "https://github.com/harshbhanushali26/ai-agent-engine/blob/main/main.py"
click node_cache "https://github.com/harshbhanushali26/ai-agent-engine/blob/main/core/memory.py"
click node_router "https://github.com/harshbhanushali26/ai-agent-engine/blob/main/core/routing/router.py"
click node_date_match "https://github.com/harshbhanushali26/ai-agent-engine/blob/main/core/routing/datetime_pattern.py"
click node_math_match "https://github.com/harshbhanushali26/ai-agent-engine/blob/main/core/routing/math_pattern.py"
click node_text_match "https://github.com/harshbhanushali26/ai-agent-engine/blob/main/core/routing/text_pattern.py"
click node_agent "https://github.com/harshbhanushali26/ai-agent-engine/blob/main/core/agent.py"
click node_planner "https://github.com/harshbhanushali26/ai-agent-engine/blob/main/core/planner.py"
click node_validator "https://github.com/harshbhanushali26/ai-agent-engine/blob/main/core/planner_validator.py"
click node_executor "https://github.com/harshbhanushali26/ai-agent-engine/blob/main/core/executor.py"
click node_runner "https://github.com/harshbhanushali26/ai-agent-engine/blob/main/app/runner.py"
click node_failure "https://github.com/harshbhanushali26/ai-agent-engine/blob/main/core/failure_classifier.py"
click node_replanner "https://github.com/harshbhanushali26/ai-agent-engine/blob/main/core/replanner.py"
click node_state "https://github.com/harshbhanushali26/ai-agent-engine/blob/main/core/state.py"
click node_responder "https://github.com/harshbhanushali26/ai-agent-engine/blob/main/core/responder.py"
click node_registry "https://github.com/harshbhanushali26/ai-agent-engine/blob/main/tools/registry.py"
click node_calculator "https://github.com/harshbhanushali26/ai-agent-engine/blob/main/tools/math/calculate.py"
click node_datetime "https://github.com/harshbhanushali26/ai-agent-engine/blob/main/tools/time/datetime.py"
click node_text_tools "https://github.com/harshbhanushali26/ai-agent-engine/blob/main/tools/text/text_transform.py"
click node_web "https://github.com/harshbhanushali26/ai-agent-engine/tree/main/tools/web"
click node_extraction "https://github.com/harshbhanushali26/ai-agent-engine/blob/main/tools/text/extract_text.py"
click node_rag "https://github.com/harshbhanushali26/ai-agent-engine/blob/main/tools/rag/knowledge_base.py"
click node_usage "https://github.com/harshbhanushali26/ai-agent-engine/blob/main/tools/usage_tracker.py"
click node_schemas "https://github.com/harshbhanushali26/ai-agent-engine/blob/main/tools/schemas.py"
click node_tool_response "https://github.com/harshbhanushali26/ai-agent-engine/blob/main/tools/responses.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_cache,node_user toneBlue
class node_router,node_date_match,node_math_match,node_text_match toneAmber
class node_agent,node_planner,node_validator,node_executor,node_runner,node_failure,node_replanner,node_state,node_responder toneMint
class node_registry,node_calculator,node_datetime,node_text_tools,node_web,node_extraction,node_rag toneRose
class node_usage,node_schemas,node_tool_response,node_llm toneIndigo
```

## Components

### Query entry

- [Interactive CLI](https://github.com/harshbhanushali26/ai-agent-engine/blob/main/main.py): `main.py`
- [Query cache](https://github.com/harshbhanushali26/ai-agent-engine/blob/main/core/memory.py): `core/memory.py`

### Fast paths

- [Pattern router](https://github.com/harshbhanushali26/ai-agent-engine/blob/main/core/routing/router.py): `core/routing/router.py`
- [Date matcher](https://github.com/harshbhanushali26/ai-agent-engine/blob/main/core/routing/datetime_pattern.py): `core/routing/datetime_pattern.py`
- [Math matcher](https://github.com/harshbhanushali26/ai-agent-engine/blob/main/core/routing/math_pattern.py): `core/routing/math_pattern.py`
- [Text matcher](https://github.com/harshbhanushali26/ai-agent-engine/blob/main/core/routing/text_pattern.py): `core/routing/text_pattern.py`

### Agent workflow

- [Agent orchestrator](https://github.com/harshbhanushali26/ai-agent-engine/blob/main/core/agent.py): `core/agent.py`
- [Plan generation](https://github.com/harshbhanushali26/ai-agent-engine/blob/main/core/planner.py): `core/planner.py`
- [Plan validation](https://github.com/harshbhanushali26/ai-agent-engine/blob/main/core/planner_validator.py): `core/planner_validator.py`
- [Step execution](https://github.com/harshbhanushali26/ai-agent-engine/blob/main/core/executor.py): `core/executor.py`
- [Tool runner](https://github.com/harshbhanushali26/ai-agent-engine/blob/main/app/runner.py): `app/runner.py`
- [Failure classifier](https://github.com/harshbhanushali26/ai-agent-engine/blob/main/core/failure_classifier.py): `core/failure_classifier.py`
- [Plan recovery](https://github.com/harshbhanushali26/ai-agent-engine/blob/main/core/replanner.py): `core/replanner.py`
- [Execution state](https://github.com/harshbhanushali26/ai-agent-engine/blob/main/core/state.py): `core/state.py`
- [Response generation](https://github.com/harshbhanushali26/ai-agent-engine/blob/main/core/responder.py): `core/responder.py`

### Tool capabilities

- [Tool registry](https://github.com/harshbhanushali26/ai-agent-engine/blob/main/tools/registry.py): `tools/registry.py`
- [Safe calculator](https://github.com/harshbhanushali26/ai-agent-engine/blob/main/tools/math/calculate.py): `tools/math/calculate.py`
- [Date-time tools](https://github.com/harshbhanushali26/ai-agent-engine/blob/main/tools/time/datetime.py): `tools/time/datetime.py`
- [Text operations](https://github.com/harshbhanushali26/ai-agent-engine/blob/main/tools/text/text_transform.py): `tools/text/text_transform.py`
- [Search and weather](https://github.com/harshbhanushali26/ai-agent-engine/tree/main/tools/web): `tools/web`
- [Text extraction](https://github.com/harshbhanushali26/ai-agent-engine/blob/main/tools/text/extract_text.py): `tools/text/extract_text.py`
- [Preference retrieval](https://github.com/harshbhanushali26/ai-agent-engine/blob/main/tools/rag/knowledge_base.py): `tools/rag/knowledge_base.py`

### Usage and contracts

- [Quota and cost tracking](https://github.com/harshbhanushali26/ai-agent-engine/blob/main/tools/usage_tracker.py): `tools/usage_tracker.py`
- [Tool contracts](https://github.com/harshbhanushali26/ai-agent-engine/blob/main/tools/schemas.py): `tools/schemas.py`
- [Tool response format](https://github.com/harshbhanushali26/ai-agent-engine/blob/main/tools/responses.py): `tools/responses.py`

### Other components

- **User**
- **LLM service**

## Connections

- User → Interactive CLI: submits query
- Interactive CLI → Query cache: checks cache (evidence: `main.py`)
- Interactive CLI → Pattern router: routes query (evidence: `main.py`)
- Interactive CLI → Agent orchestrator: sends cache miss (evidence: `main.py`)
- Interactive CLI → Quota and cost tracking: checks quota (evidence: `main.py`)
- Pattern router → Date matcher: checks dates (evidence: `core/routing/router.py`)
- Pattern router → Math matcher: checks math (evidence: `core/routing/router.py`)
- Pattern router → Text matcher: checks text (evidence: `core/routing/router.py`)
- Date matcher → Date-time tools: uses operations (evidence: `core/routing/datetime_pattern.py`)
- Math matcher → Safe calculator: evaluates expression (evidence: `core/routing/math_pattern.py`)
- Text matcher → Text operations: uses operations (evidence: `core/routing/text_pattern.py`)
- Agent orchestrator → Plan generation: requests plan (evidence: `core/agent.py`)
- Agent orchestrator → Plan validation: validates plan (evidence: `core/agent.py`)
- Agent orchestrator → Step execution: executes plan (evidence: `core/agent.py`)
- Agent orchestrator → Response generation: generates response (evidence: `core/agent.py`)
- Agent orchestrator → Failure classifier: classifies failure (evidence: `core/agent.py`)
- Agent orchestrator → Plan recovery: repairs plan (evidence: `core/agent.py`)
- Step execution → Tool runner: runs steps (evidence: `core/executor.py`)
- Step execution → Execution state: resolves dependencies (evidence: `core/executor.py`)
- Tool runner → Tool registry: dispatches tools (evidence: `app/runner.py`)
- Plan recovery → Plan generation: requests replacement plan (evidence: `core/replanner.py`)
- Plan recovery → Plan validation: validates replacement (evidence: `core/replanner.py`)
- Plan generation → LLM service: requests completion (evidence: `core/planner.py`)
- Response generation → LLM service: requests completion (evidence: `core/responder.py`)
- Tool registry → Safe calculator: registers handler (evidence: `tools/registry.py`)
- Tool registry → Date-time tools: registers handlers (evidence: `tools/registry.py`)
- Tool registry → Text operations: registers handler (evidence: `tools/registry.py`)
- Tool registry → Search and weather: registers handlers (evidence: `tools/registry.py`)
- Tool registry → Text extraction: registers handler (evidence: `tools/registry.py`)
- Tool registry → Preference retrieval: registers handler (evidence: `tools/registry.py`)
- Interactive CLI → Preference retrieval: retrieves context
- Agent orchestrator → Quota and cost tracking: tracks calls (evidence: `core/agent.py`)
- Plan generation → Tool contracts: uses contracts (evidence: `core/planner.py`)
- Plan validation → Tool registry: checks tool catalog (evidence: `core/planner_validator.py`)
- Safe calculator → Tool response format: formats result (evidence: `tools/math/calculate.py`)
- Date-time tools → Tool response format: formats result (evidence: `tools/time/datetime.py`)
- Response generation → Tool contracts: uses contracts (evidence: `core/responder.py`)
