Agronomic decision-support AI agent for Mato Grosso cotton and soybean operations (Fazenda Karasu — Sorriso/MT), built from scratch around an explicit, framework-free Agentic Loop (no LangChain, LlamaIndex, or CrewAI).
src/karasu_scout/
├── domain/
│ ├── models.py # Pure Pydantic v2 models (plots, pests, weather, work orders, traces)
│ └── agronomy_rules.py # Deterministic GDD, pest threshold, and spray window calculations
├── ports/
│ ├── llm.py # LLMClient protocol
│ └── approval.py # ApprovalHandler protocol (Human-in-the-Loop gate)
├── engine/
│ ├── registry.py # ToolRegistry: OpenAI JSON schema generation + safe dispatch
│ └── harness.py # AgentHarness: explicit loop, short-term memory, HITL gate, trace events
├── adapters/
│ ├── farm_repository.py # Mato Grosso cotton & soybean plots, scouting logs, weather windows
│ ├── openai_client.py # OpenAI SDK adapter implementing LLMClient
│ └── sqlite_store.py # SQLiteAuditStore for trace_events and spray work_orders
├── tools/
│ └── agronomy_tools.py # 7 registered tools (6 read/calc + 1 HITL-gated work order tool)
└── cli.py # RichTraceRenderer, CLIApprovalHandler, interactive REPL & --dry-run
- Explicit Agentic Loop: Bounded by
max_steps, emitting structuredTraceEventtelemetry (USER_INPUT,LLM_REQUEST,LLM_TOOL_CALL,TOOL_APPROVAL_REQUESTED,TOOL_APPROVAL_DENIED,TOOL_EXECUTED,TOOL_ERROR,LLM_TEXT_REPLY,MAX_STEPS_EXCEEDED). - Deterministic Agronomy Rules: Growing Degree Days (
calculate_gdd), pest control thresholds (Anthonomus grandis >= 5.0% attacked squares; Euschistus heros >= 2.0/m), and operational spray window evaluation (IDEAL,MARGINAL,UNFAVORABLE) are never delegated to LLM math. - Human-in-the-Loop (HITL) Approval Gate: Mutating tools (
issue_spray_work_order) require explicit operator approval viaApprovalHandlerbefore execution. Denials feedREJECTED_BY_OPERATORback into the loop so the model adapts cleanly. - SQLite Audit & Persistence: Every trace event and approved
SprayWorkOrderis persisted to SQLite (karasu_scout.dbby default).
Requires Python 3.12+ and uv.
uv syncConfigure environment variables (e.g., in .env) for live OpenAI mode:
OPENAI_API_KEY=sk-...
# Optional:
# OPENAI_BASE_URL=https://api.openai.com/v1
# KARASU_MODEL=gpt-4o-miniRun the scripted multi-step demo without an API key:
uv run karasu-scout --dry-run --auto-approveRun the interactive REPL:
uv run karasu-scoutCLI flags & REPL commands:
--dry-run: Run the built-inDemoScriptedLLMClientscenario.--db-path PATH: SQLite database path (default:karasu_scout.db).--auto-approve: Skip interactive[y/N]confirmation for mutating tools./orders: Display persistedSprayWorkOrderrecords from SQLite./reset: Clear short-term conversation memory and start a new session ID./exit: Quit the REPL.
make check # ruff check + ruff format --check + pyright (strict) + pytest
make cov # pytest with line coverage report
make ci # make check + pip-audit