LangGraph Workflow Powers Reliable Agent Loops

Connect Groq's OpenAI-compatible endpoint (base_url="https://api.groq.com/openai/v1") to ChatOpenAI with model="llama-3.3-70b-versatile" and temperature=0.3, binding all tools for tool-calling. Use StateGraph with AgentState (messages: AnnotatedSequenceBaseMessage, add_messages) to alternate agent reasoning and ToolNode execution: entry at "agent", conditional edge from "agent" (tools if tool_calls else END), edge "tools"→"agent". Set recursion_limit=50 (2x max_steps=25) in .stream() to prevent infinite loops. This setup handles multi-turn reasoning without state explosion, as sub-agents run isolated.

Lead system prompt enforces: list_skills/load_skill for complex tasks; spawn_subagent for subtasks; persist to workspace/outputs/; remember() for cross-run facts. Run function streams updates, logging tool calls (e.g., 01 🔧 web_search({query})), agent responses, and tool outputs, then dumps sandbox file_list(), recall(), and outputs/ files—reveals ~400-word reports with exec summary, findings, analysis, sources.

Trade-off: Groq's speed (free tier) trades slight quality for llama-3.3 vs. GPT-4o, but tool-binding + low temp=0.2/0.3 ensures structured outputs without hallucinations.

Sandboxed Tools Enable Safe Web/File/Code Access

Restrict to SANDBOX=/content/deerflow_sandbox with _safe() path validation to prevent escapes. Core tools:

  • Search/Fetch: web_search(query, max_results=5) via DDGS returns title/URL/snippet; web_fetch(url, max_chars=4000) strips scripts/nav with BeautifulSoup, cleans whitespace.
  • Files: file_write/read/list(path) limits read to 8KB, lists 60 rglob items (skip memory/), mkdirs parents.
  • Code: python_exec(code) in isolated globals (SANDBOX_ROOT preset), captures stdout/stderr to 4KB, artifacts to outputs/—plan in English first, verify results.
  • Memory: remember(fact) appends timestamped JSON to memory/long_term.json (facts, preferences{}); recall() shows last 20.

These give controlled REPL-like access: agent computes charts, cross-refs sources (claim→evidence→URL), without sys/network risks. Bind BASE_TOOLS=list_skills,load_skill,... + spawn_subagent to llm.

Skills and Sub-Agents Modularize Complex Research

Pre-register SKILL.md files (public/custom/): research (decompose to 3-5 sub-questions, 2 authoritative URLs each, cross-ref, append workspace/research_notes.md); report-generation (read notes, outline exec summary (3-5 sentences)/findings/analysis/conclusion/sources, write outputs/report.md); code-execution (plan→exec→verify).

Agent calls list_skills()→load_skill(name) to discover/execute workflows. spawn_subagent(role,task,allowed_tools="web_search,web_fetch,file_write,file_read") creates isolated ChatOpenAI(temp=0.2, bind sub_tools), sys prompt mandates 'FINAL REPORT:' ≤700-word summary. Loops 8 steps max, returns report—keeps lead agent lean for coordination.

Demo task: (1) discover skills; (2) sub-agent researches 3 SLMs (2024-2025 sizes/benchmarks/use-cases)→workspace/slm_research.md; (3) load report-generation→outputs/slm_briefing.md; (4) remember(key takeaway); (5) summarize. Persists across runs via JSON memory, outputs structured MD with numbered sources—scales to briefings/automation.

Extend by adding skills (e.g., data viz), scoping sub-agent tools, or integrating uploads/.