How moh4696/build-ai-agents-free works
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: agent.py Complete agent · .env.example Credential template · 01_first_agent.py Basic chat · 02_agent_with_tools.py Tool calling
Components 17
Complete Runtime
Runnable terminal agent integration.
- Terminal user
- Complete agent · Loads configuration, selects a provider, invokes the agent, and prints the result.
src/agent.py - Environment loader · Loads local credentials at startup.
- Credential template
.env.example - LangChain agent · Combines model, system prompt, tools, and checkpointing.
- Plan / act / observe · The model can call tools before producing its final message.
- Thread memory · Process-local history is isolated by configurable.thread_id.
- Terminal output · Final assistant message.
- Word count
src/agent.py
External Providers
- Groq Llama · Primary inference provider.
- Google Gemini · Fallback provider selected after a failed Groq probe.
- DuckDuckGo search · External tool that may fail independently of inference.
Learning Stages
Standalone progressive examples.
- Basic chat · Introduces create_agent with a model and prompt.
src/01_first_agent.py - Tool calling · Introduces search and local tools.
src/02_agent_with_tools.py - Conversation memory · Introduces thread-scoped checkpointing.
src/03_agent_with_memory.py - Provider fallback · Introduces Groq probing and Gemini fallback.
src/04_agent_with_fallback.py
Other components
- Python dependencies · Python 3.10+ LangChain, LangGraph, provider, and dotenv dependencies.
requirements.txt
Connections 19
- 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