A hand-calculated and programmatically verified implementation of a pure Python ReAct Agent loop, vector search routing, memory stack tracking, and cost analytics. This repository mirrors a six-page worksheet workflow with handwritten scans in assets/ and a direct math-first software implementation in src/.
This project is built in the spirit of the first-principles notebook practice popularized by Andrej Karpathy and the "AI by Hand" movement. The goal is to make the internal mechanics of an agent explicit, transparent, and reproducible without relying on abstraction layers such as LangChain or other agent frameworks.
The scenario is a comic-book universe network query:
- User query: "Find if Batman has a hidden connection to Spider-Man through an intermediate contact."
- Target problem: route the query through a small directed knowledge graph, choose a specialized tool, and build the step-by-step ReAct prompt trace.
This repository demonstrates:
- Manual vector routing via explicit cosine similarity.
- A deterministic graph lookup environment.
- Append-only prompt buffer growth tracking.
- Word-count-based cost modeling.
- Automated analytics chart generation.
Each page of the physical workbook is paired below with its high-resolution scan alongside clean digital transcriptions, tables, and mathematical references for maximum accessibility.
- Description: High-level system architecture and the 4-node directed knowledge graph layer.
- Code implementation:
src/environment.py
[User Query] -> [Prompt Context Window Buffer (S_t)]
β β²
βΌ β
[LLM Reasoning] β
β β
βΌ β
[Tool Selection] --βΌ-- [Vector Space Match]
β β
βΌ β
[Tool Execution] --β
(Graph_Lookup)
Batman -> Superman -> Iron Man -> Spider-Man
-
Description: The agent evaluates its tool registry and chooses between a Calculator tool and a
Graph_Lookuptool using a hard-coded 2D vector space. - Selection rule: $\text{Selected Tool} = \text{argmax}(\text{Similarity})$ over cosine similarity values.
-
Code implementation:
src/embeddings.py
- Query:
$\vec{q} = [0.10, 0.90]$ - Calculator tool:
$\vec{t}_0 = [0.95, 0.05]$ -
Graph_Lookuptool:$\vec{t}_1 = [0.15, 0.85]$
| Pair | Dot Product | Cosine Similarity | Decision | ||
|---|---|---|---|---|---|
| Query vs Calculator | 0.1400 | 0.9055 | 0.9513 | 0.1625 | Rejected |
Query vs Graph_Lookup
|
0.7800 | 0.9055 | 0.8631 | 0.9980 | Selected |
- Description: Tracking the append-only prompt buffer across generation steps, actions, and tool observations.
- Code implementation:
src/agent.py
-
$t = 0$ Initial prompt: "User Query: Find if Batman has a hidden connection to Spider-Man through an intermediate contact. Available Tools:Graph_Lookup(character_name)which returns immediate neighbors." -
$t = 1$ Thought + action: "I need to check who Batman is directly connected to first.Graph_Lookup("Batman")" -
$t = 2$ Observation: "Neighbors of Batman = [Superman]" -
$t = 3$ Thought + action: "Batman connects to Superman. Now I need to see if Superman connects closer to Spider-Man or to someone else who does.Graph_Lookup("Superman")" -
$t = 4$ Observation: "Neighbors of Superman = [Iron Man]" -
$t = 5$ Final resolution bundle: "Superman connects to Iron Man. Let me check Iron Man's connections.Graph_Lookup("Iron Man")Neighbors of Iron Man = [Spider-Man] Iron Man connects directly to Spider-Man. Path found: Batman -> Superman -> Iron Man -> Spider-Man. Yes, Batman has a hidden connection to Spider-Man. The path is: Batman -> Superman -> Iron Man -> Spider-Man."
| Timestep | Component | Words Added | Cumulative Volume |
|---|---|---|---|
| 0 | Initial prompt |
22 | 22 |
| 1 | Generation block |
12 | 34 |
| 2 | Observation |
5 | 39 |
| 3 | Generation block |
22 | 61 |
| 4 | Observation |
6 | 67 |
| 5 | Final bundle BUNDLE_5
|
49 | 116 |
- Description: System diagnostics that quantify context growth formulas, path efficiency, and turn-level operational expense.
- Code implementation:
src/metrics.py
- Input cost: $C_{in} =
$0.001$ per word - Output cost: $C_{out} =
$0.003$ per word - Billing formula: $\text{Cost}_t = (\text{Input}t \times C{in}) + (\text{Output}t \times C{out})$
| Metric | Formula | Value |
|---|---|---|
| Context Explosion Ratio |
5.27 | |
| Tool Trajectory Efficiency |
1.00 | |
| Turn 1 cost (t=1) | ||
| Turn 2 cost (t=3) | ||
| Turn 3 cost (t=5) | ||
| Total trajectory cost | Sum of turn costs |
Run the implementation to verify the handwritten calculations and generate analytics visualizations.
pip install -r requirements.txtpython main.pyThe script produces a terminal log showing:
- cosine similarity values for tool routing,
- exact word counts for every prompt step,
- cumulative context volumes,
- cost estimates per turn,
- and chart files saved into
assets/.
The values shown in the log are printed with four-decimal precision using f"{sim:.4f}" so the reported similarity values match the explicit geometric calculations in src/embeddings.py.
======================================================================
PHASE A: INITIAL VECTOR ROUTING SEARCH
======================================================================
Calculating tool semantic weights via explicit Cosine Similarity...
- Vector Match [Query vs. Calculator Tool]: 0.1625
- Vector Match [Query vs. Graph_Lookup Tool]: 0.9980
>>> Decision Engine Output: Selected Tool 1 (Graph_Lookup Tool) [ARGMAX SUCCESS]
======================================================================
PHASE B & C: EXECUTION TRACE & TEXT VOLUMETRICS
======================================================================
[t = 0] Initial Context Window Payload Size (S0): 22 words.
[t = 1] Generation Cycle 1 Engine Block (T0 + A0) appended: 12 words.
[t = 2] Environment Input Step (O1) appended: 5 words. -> Payload Size: 39 words.
[t = 3] Generation Cycle 2 Engine Block (T1 + A1) appended: 22 words.
[t = 4] Environment Input Step (O2) appended: 6 words. -> Payload Size: 67 words.
[t = 5] Final Processing Sequence appended: 49 words.
Final Response (R): Yes, Batman has a hidden connection to Spider-Man.
The path is: Batman -> Superman -> Iron Man -> Spider-Man.
Tool validation from src/environment.py:
- graph_lookup('Batman') => ['Superman']
- graph_lookup('Superman') => ['Iron Man']
- graph_lookup('Iron Man') => ['Spider-Man']
======================================================================
PHASE D: SYSTEM DIAGNOSTICS & ANALYTICS REPORT
======================================================================
- Context Explosion Ratio (rho) : 5.27x (Payload expanded 527.27%)
- Tool Trajectory Efficiency (eta): 1.00 (Perfect path sequence achieved)
Financial Billing Breakdown per Turn:
- Turn 1 Operational Expense (t=1): $0.0580
- Turn 2 Operational Expense (t=3): $0.1050
- Turn 3 Operational Expense (t=5): $0.2140
----------------------------------------------------------------------
>>> TOTAL AGENTIC TRAJECTORY COST: $0.3770
======================================================================
System charts successfully generated and saved to 'assets/' folder.
assets/context_growth.pngassets/cost_per_step.pngassets/dashboard_output.png
You can use these charts as the production-ready dashboard output for the final analytics page.
Mechanistic reasoning reveals the true cost and complexity of an agentic loop. It helps explain why sequential reasoning with an append-only context window naturally increases memory usage and inference expense over time.
- Context explosion is real: The prompt window grows at each step, forcing later turns to reprocess all prior state.
- Tool selection can be explicit: Cosine similarity in a small embedding space is enough to choose the correct tool for this scenario.
- ReAct is reproducible: The exact thought/action/observation trace can be expressed as plain strings and validated with code.
- Cost modeling matters: Explicit per-turn input/output pricing makes the production economics of an agent visible.
- Yao, S., Zhang, D., Bansal, M., & Liang, P. (2022). ReAct: Synergizing Reasoning and Acting in Language Models.
- Karpathy, A. β inspiration from step-by-step low-level AI implementation and clear code-first education.
- Yeh, T. β inspiration from the "AI by Hand" first-principles workflow.
.
βββ .gitignore
βββ README.md
βββ requirements.txt
βββ assets/
β βββ dashboard_output.png
β βββ page1_architecture.jpg
β βββ page2_phase_a_routing.jpg
β βββ page3_worktable_2_1.jpg
β βββ page4_phases_b_c_ledger.jpg
β βββ page5_worktable_3_1.jpg
β βββ page6_phase_d_analytics.jpg
βββ src/
β βββ __init__.py
β βββ agent.py
β βββ embeddings.py
β βββ environment.py
β βββ metrics.py
βββ main.py






