# moh4696/build-ai-agents-free architecture

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

- Interactive diagram: https://gitdiagram.com/moh4696/build-ai-agents-free
- Repository: https://github.com/moh4696/build-ai-agents-free

## Overview

#### Purpose and Entry Points
This is a tutorial-style Python repository showing progressive construction of a free LLM agent: basic chat, tool use, conversation memory, and provider fallback. Scripts are standalone terminal entry points; the complete runnable integration is `src/agent.py`. Learning stages are in `src/01_first_agent.py` through `src/04_agent_with_fallback.py`.

#### Agent Construction Layer
Each stage uses LangChain’s `create_agent` to assemble a model, tool set, system prompt, and optionally a LangGraph checkpointer. Invocation supplies a LangChain message payload and reads the final returned message.
- `src/01_first_agent.py`
- `src/agent.py`

#### Model Provider Boundary and Failover
The primary inference provider is Groq via `ChatGroq` and its hosted Llama model. The fallback stage probes Groq with a lightweight request, catches provider/runtime failures, and instead constructs a Gemini `ChatGoogleGenerativeAI` model. This is initialization-time failover: once selected, the agent runs on that provider for the process.
- `src/04_agent_with_fallback.py`
- `src/agent.py`
- `.env.example`

#### Tool-Calling Agent Loop
The tool-enabled agent exposes a DuckDuckGo web-search adapter plus a local `word_count` function decorated as a LangChain tool. The model selects tools during the agent’s plan/act/observe loop, receives their results, and produces a final response. Tool execution is a key external boundary: search may be rate-limited or fail independently of the model.
- `src/02_agent_with_tools.py`
- `src/agent.py`

#### Conversation State and Thread Isolation
Memory is implemented through LangGraph’s `InMemorySaver` checkpointer. Calls sharing `configurable.thread_id` reuse stored message history; different IDs isolate conversations. State is process-local RAM only, so it is lost on restart and has no durable persistence layer.
- `src/03_agent_with_memory.py`
- `src/agent.py`

#### End-to-End Control Flow
At startup, scripts load environment variables, select a working model provider, construct the agent with prompt/tools/checkpointer, then invoke it with user messages. In the complete agent, request handling may use DuckDuckGo and the local counting tool, updates in-memory state under a thread ID, and prints the final assistant message to stdout.
- `src/agent.py`
- `src/02_agent_with_tools.py`
- `src/03_agent_with_memory.py`

#### Runtime and Dependencies
The runtime is Python 3.10+ run locally. Core infrastructure is LangChain/LangGraph; `python-dotenv` loads secrets without embedding them in code. External services are Groq, optional Google Gemini, and DuckDuckGo search. Dependencies are declared in `requirements.txt`; credentials are expected from ignored local `.env` files, with template guidance in `.env.example`.
- `requirements.txt`
- `.env.example`
- `src/agent.py>

Main entry points: [`src/agent.py`](https://github.com/moh4696/build-ai-agents-free/blob/master/src/agent.py) (Complete agent), [`.env.example`](https://github.com/moh4696/build-ai-agents-free/blob/master/.env.example) (Credential template), [`src/01_first_agent.py`](https://github.com/moh4696/build-ai-agents-free/blob/master/src/01_first_agent.py) (Basic chat), [`src/02_agent_with_tools.py`](https://github.com/moh4696/build-ai-agents-free/blob/master/src/02_agent_with_tools.py) (Tool calling)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Complete Runtime"]
  node_terminal(("Terminal user<br/>CLI input"))
  node_complete_agent["Complete agent<br/>Python entry point<br/>[agent.py]"]
  node_dotenv["Environment loader<br/>python-dotenv"]
  node_env_template["Credential template<br/>environment guide<br/>[.env.example]"]
  node_agent_factory["LangChain agent<br/>create_agent"]
  node_agent_loop{{"Plan / act / observe<br/>agent loop"}}
  node_memory[("Thread memory<br/>LangGraph InMemorySaver")]
  node_stdout(("Terminal output<br/>stdout"))
  node_word_count["Word count<br/>local LangChain tool<br/>[agent.py]"]
end

subgraph group_providers["External Providers"]
  node_groq["Groq Llama<br/>ChatGroq provider"]
  node_gemini["Google Gemini<br/>ChatGoogleGenerativeAI"]
  node_search["DuckDuckGo search<br/>web-search tool"]
end

subgraph group_tutorials["Learning Stages"]
  node_first_stage["Basic chat<br/>tutorial script<br/>[01_first_agent.py]"]
  node_tools_stage["Tool calling<br/>tutorial script"]
  node_memory_stage["Conversation memory<br/>tutorial script"]
  node_fallback_stage["Provider fallback<br/>tutorial script"]
end

node_dependencies["Python dependencies<br/>requirements manifest<br/>[requirements.txt]"]

node_terminal -->|"user message"| node_complete_agent
node_env_template -.->|"documents variables"| node_dotenv
node_dotenv -->|"loads credentials"| node_complete_agent
node_complete_agent -->|"probe and select primary"| node_groq
node_complete_agent -.->|"on Groq failure"| node_gemini
node_complete_agent -->|"constructs"| node_agent_factory
node_groq -->|"selected model"| node_agent_factory
node_gemini -.->|"fallback model"| node_agent_factory
node_agent_factory -->|"invokes"| node_agent_loop
node_agent_factory -->|"uses checkpointer"| node_memory
node_agent_loop -->|"tool call"| node_search
node_agent_loop -->|"tool call"| node_word_count
node_agent_loop -->|"reads and updates by thread ID"| node_memory
node_agent_loop -->|"final response"| node_stdout
node_first_stage -.->|"teaches"| node_agent_factory
node_tools_stage -.->|"teaches"| node_agent_loop
node_memory_stage -.->|"teaches"| node_memory
node_fallback_stage -.->|"demonstrates fallback"| node_gemini
node_dependencies -.->|"runtime packages"| node_complete_agent

click node_complete_agent "https://github.com/moh4696/build-ai-agents-free/blob/master/src/agent.py"
click node_env_template "https://github.com/moh4696/build-ai-agents-free/blob/master/.env.example"
click node_word_count "https://github.com/moh4696/build-ai-agents-free/blob/master/src/agent.py"
click node_first_stage "https://github.com/moh4696/build-ai-agents-free/blob/master/src/01_first_agent.py"
click node_tools_stage "https://github.com/moh4696/build-ai-agents-free/blob/master/src/02_agent_with_tools.py"
click node_memory_stage "https://github.com/moh4696/build-ai-agents-free/blob/master/src/03_agent_with_memory.py"
click node_fallback_stage "https://github.com/moh4696/build-ai-agents-free/blob/master/src/04_agent_with_fallback.py"
click node_dependencies "https://github.com/moh4696/build-ai-agents-free/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_terminal,node_complete_agent,node_dotenv,node_env_template,node_agent_factory,node_agent_loop,node_memory,node_stdout,node_word_count toneBlue
class node_groq,node_gemini,node_search toneAmber
class node_first_stage,node_tools_stage,node_memory_stage,node_fallback_stage toneMint
class node_dependencies toneNeutral
```

## Components

### Complete Runtime

Runnable terminal agent integration.

- **Terminal user**: CLI input
- [Complete agent](https://github.com/moh4696/build-ai-agents-free/blob/master/src/agent.py): Python entry point · Loads configuration, selects a provider, invokes the agent, and prints the result. · `src/agent.py`
- **Environment loader**: python-dotenv · Loads local credentials at startup.
- [Credential template](https://github.com/moh4696/build-ai-agents-free/blob/master/.env.example): environment guide · `.env.example`
- **LangChain agent**: create_agent · Combines model, system prompt, tools, and checkpointing.
- **Plan / act / observe**: agent loop · The model can call tools before producing its final message.
- **Thread memory**: LangGraph InMemorySaver · Process-local history is isolated by configurable.thread_id.
- **Terminal output**: stdout · Final assistant message.
- [Word count](https://github.com/moh4696/build-ai-agents-free/blob/master/src/agent.py): local LangChain tool · `src/agent.py`

### External Providers

- **Groq Llama**: ChatGroq provider · Primary inference provider.
- **Google Gemini**: ChatGoogleGenerativeAI · Fallback provider selected after a failed Groq probe.
- **DuckDuckGo search**: web-search tool · External tool that may fail independently of inference.

### Learning Stages

Standalone progressive examples.

- [Basic chat](https://github.com/moh4696/build-ai-agents-free/blob/master/src/01_first_agent.py): tutorial script · Introduces create_agent with a model and prompt. · `src/01_first_agent.py`
- [Tool calling](https://github.com/moh4696/build-ai-agents-free/blob/master/src/02_agent_with_tools.py): tutorial script · Introduces search and local tools. · `src/02_agent_with_tools.py`
- [Conversation memory](https://github.com/moh4696/build-ai-agents-free/blob/master/src/03_agent_with_memory.py): tutorial script · Introduces thread-scoped checkpointing. · `src/03_agent_with_memory.py`
- [Provider fallback](https://github.com/moh4696/build-ai-agents-free/blob/master/src/04_agent_with_fallback.py): tutorial script · Introduces Groq probing and Gemini fallback. · `src/04_agent_with_fallback.py`

### Other components

- [Python dependencies](https://github.com/moh4696/build-ai-agents-free/blob/master/requirements.txt): requirements manifest · Python 3.10+ LangChain, LangGraph, provider, and dotenv dependencies. · `requirements.txt`

## Connections

- Terminal user → Complete agent: user message
- Credential template → Environment loader: documents variables
- Environment loader → Complete agent: loads credentials
- Complete agent → Groq Llama: probe and select primary
- Complete agent → Google Gemini: on Groq failure · Fallback is chosen once during initialization.
- Complete agent → LangChain agent: constructs
- Groq Llama → LangChain agent: selected model
- Google Gemini → LangChain agent: fallback model
- LangChain agent → Plan / act / observe: invokes
- LangChain agent → Thread memory: uses checkpointer
- Plan / act / observe → DuckDuckGo search: tool call
- Plan / act / observe → Word count: tool call
- Plan / act / observe → Thread memory: reads and updates by thread ID
- Plan / act / observe → Terminal output: final response
- Basic chat → LangChain agent: teaches
- Tool calling → Plan / act / observe: teaches
- Conversation memory → Thread memory: teaches
- Provider fallback → Google Gemini: demonstrates fallback
- Python dependencies → Complete agent: runtime packages
