15 The code — setup and shared pattern
All five agents live in 02_agents_code/, share one .venv and follow the same file pattern (llm.py, tools.py, agents.py, main.py). This chapter shows the setup files; the following chapters list every source file of each agent.
15.1 02_agents_code/README.md
15.1.1 README.md
# 02_agents_code — Five independent AI research agents (Python)
One shared virtual environment (`.venv`), one `.env` with your API key, five **self-contained** agent folders.
No agent imports from another agent — copy any folder out and it still runs.
```
02_agents_code/
├── .venv/ ← shared virtual environment (created by setup script)
├── requirements.txt ← all dependencies for all five agents
├── .env.example → .env ← ANTHROPIC_API_KEY, CLAUDE_MODEL, optional search keys
├── setup_venv.ps1 / setup_venv.sh ← one-shot setup
├── agent_01_research_discovery/ ← topic → papers → gaps → ideas → proposal
├── agent_02_literature_review/ ← question → PRISMA search/screen/extract → review
├── agent_03_research_experiment/ ← design → analysis (code) → stats review → methods + notebook
├── agent_04_academic_writing/ ← notes → manuscript → journal match → reviewer sim → rebuttal
└── agent_05_phd_supervisor/ ← roadmap → weekly progress → critique → presentation (memory)
```
## Setup (Windows PowerShell)
```powershell
cd 02_agents_code
.\setup_venv.ps1 # creates .venv, installs requirements, copies .env.example → .env
notepad .env # paste your ANTHROPIC_API_KEY
.\.venv\Scripts\Activate.ps1
```
macOS / Linux: `bash setup_venv.sh && source .venv/bin/activate`
## Run each agent
```powershell
python agent_01_research_discovery\main.py --topic "AI-based drought prediction in wheat"
python agent_02_literature_review\main.py --question "AI methods for plant disease detection"
python agent_03_research_experiment\main.py --description "RNA-seq of heat-stressed rice, control vs heat, n=3" --demo
python agent_04_academic_writing\main.py --notes agent_04_academic_writing\sample_notes.md --field "plant science"
python agent_05_phd_supervisor\main.py init --student "Ali" --topic "Multimodal AI for drought resilience in wheat"
```
Add `--help` to any command to see all options. Agents 1 and 2 accept `--offline` (built-in demo papers); Agent 3 accepts `--no-llm` (numeric pipeline only, no API key needed).
## The shared pattern inside every agent
| File | Role |
|------|------|
| `llm.py` | `ask()` — streamed free text; `ask_structured()` — JSON constrained to a Pydantic model. Model = `CLAUDE_MODEL` env var (default `claude-opus-5`), adaptive thinking, effort per call. |
| `tools.py` | Deterministic functions the agent calls (search APIs, data analysis, memory) |
| `agents.py` | Small single-responsibility agents: a system prompt + one call + a Pydantic output model |
| `main.py` | A Typer CLI that orchestrates the agents and saves everything to `outputs/` |
### Why this design
- **Structured outputs** make agents composable and testable — every decision is data, not prose.
- **Tools own the facts** (papers, numbers); the model owns reasoning and writing → fewer hallucinations.
- **Streaming** avoids timeouts on long outputs (reviews, manuscripts).
- **One file to swap providers**: only `llm.py` knows about the API.
### Cost tips
- Set `CLAUDE_MODEL=claude-sonnet-5` in `.env` for cheap bulk runs (screening 500 abstracts), keep `claude-opus-5` for writing and critique.
- `effort` is set per call (`low` for screening/planning, `high` for gaps/critique/writing).
- Enable prompt caching on the big stable prefixes (protocol + corpus) when you move to a web app.
---
**Codanics — Python ka Chilla: Build AI Agents**
Course: https://codanics.com/courses/python-ka-chilla-build-ai-agents/ · Website: https://codanics.com · YouTube: [@codanics](https://www.youtube.com/@codanics)
WhatsApp: https://whatsapp.com/channel/0029Va7nRDq3QxRzGqaQvS3r · Free course: https://www.youtube.com/watch?v=UE3o-7KRNXs&list=PLaQmdklCcX4015.2 requirements.txt
15.2.1 requirements.txt
# Codanics - Python ka Chilla: Build AI Agents
# https://codanics.com/courses/python-ka-chilla-build-ai-agents/
#
# One shared virtual environment for all five agents.
# python -m venv .venv
# .\.venv\Scripts\Activate.ps1 (Windows) | source .venv/bin/activate (macOS/Linux)
# pip install -r requirements.txt
# --- LLM + structured outputs ---
anthropic
pydantic>=2.7
python-dotenv
# --- research search tools (free public APIs) ---
requests
arxiv
# --- CLI / console ---
typer
rich
python-slugify
# --- data, statistics, ML (Experiment Agent) ---
pandas
numpy
scipy
scikit-learn
statsmodels
matplotlib
nbformat
tabulate
# --- documents ---
pypdf
15.3 .env.example
15.3.1 .env.example
# Copy this file to .env and fill in your keys. Never commit .env.
# Required: Anthropic Claude API key (https://console.anthropic.com/)
ANTHROPIC_API_KEY=sk-ant-...
# Optional: model override (default is claude-opus-5). e.g. claude-sonnet-5 for cheaper bulk runs
CLAUDE_MODEL=claude-opus-5
# Recommended: free Semantic Scholar API key - anonymous access is often rate-limited (HTTP 429)
# https://www.semanticscholar.org/product/api
SEMANTIC_SCHOLAR_API_KEY=
# Optional but polite for PubMed E-utilities (NCBI asks for an email; API key raises rate limit)
NCBI_EMAIL=you@example.com
NCBI_API_KEY=15.4 setup_venv.ps1
15.4.1 setup_venv.ps1
# Codanics - Python ka Chilla: Build AI Agents
# Creates the shared virtual environment and installs all dependencies (Windows PowerShell).
# PS> .\setup_venv.ps1
python -m venv .venv
.\.venv\Scripts\Activate.ps1
python -m pip install --upgrade pip
pip install -r requirements.txt
if (-not (Test-Path .env)) { Copy-Item .env.example .env; Write-Host "Created .env - add your ANTHROPIC_API_KEY" }
Write-Host "Done. Try: python agent_01_research_discovery\main.py --topic 'AI-based drought prediction in wheat'"15.5 setup_venv.sh
15.5.1 setup_venv.sh
#!/usr/bin/env bash
# Codanics - Python ka Chilla: Build AI Agents
# Creates the shared virtual environment and installs all dependencies (macOS / Linux).
# $ bash setup_venv.sh
set -e
python3 -m venv .venv
source .venv/bin/activate
python -m pip install --upgrade pip
pip install -r requirements.txt
[ -f .env ] || { cp .env.example .env; echo "Created .env - add your ANTHROPIC_API_KEY"; }
echo "Done. Try: python agent_01_research_discovery/main.py --topic 'AI-based drought prediction in wheat'"15.6 .gitignore
15.6.1 .gitignore
.venv/
.env
__pycache__/
*.pyc
**/outputs/
**/memory/
*.ipynb_checkpoints