STACK IT FAST
ALL RULES & SKILLS

LangGraph + Python + FastAPI (Agentic AI Workflows)

Raw .MD langgraph-python-fastapi
CURATED RULE AGENTS.MD + CLAUDE.MD + .MDC + SKILL.MD

Production architecture for autonomous multi-agent systems, LangGraph cyclic state graphs, Pydantic v2 structured tool calling, and Chroma/Qdrant vector stores.

AGENTS.md
Paste in your project root
SKILL.md
Installs to .claude/skills/langgraph-python-fastapi
INTERACTIVE RULE & SKILL VIEWER (AGENTS.MD, CLAUDE.MD, .MDC & SKILL.MD)
Optimized for:CursorClaude CodeWindsurfAGY
AGENTS.md·43 lines · 3.3 KB
1# Project Architecture & Guidelines (LangGraph + Python + FastAPI)
2
3## 1. System Architecture
4- **Framework**: FastAPI (Async ASGI with Uvicorn).
5- **Agent Orchestration**: LangGraph (`StateGraph`, `MessagesState`) for stateful cyclic multi-agent loops.
6- **Data Validation & Tools**: Pydantic v2 (`BaseModel`, `Field`) for type validation and structured LLM tool definitions.
7- **Vector Database**: Qdrant / Chroma / pgvector for semantic retrieval augmented generation (RAG).
8- **LLM Integration**: LangChain Chat Models (`ChatOpenAI`, `ChatAnthropic`, local Ollama/vLLM endpoints).
9
10## 2. Directory & Module Organization
11- `app/api/`: FastAPI route handlers (e.g. `POST /api/chat`, `GET /api/runs/{run_id}`).
12- `app/agents/`:
13 - `graph.py`: StateGraph definition, node connections, and compiled runnable workflow.
14 - `state.py`: TypedDict or Pydantic definitions of agent memory and intermediate scratchpads.
15 - `nodes.py`: Discrete execution steps (reasoner, retriever, tool_executor, reviewer).
16- `app/tools/`: Custom Pydantic-validated tool functions decorated with `@tool`.
17- `app/core/`: Configuration, LLM client singleton, and OpenTelemetry / LangSmith tracing.
18
19## 3. Agent Graph Guardrails
20- Always bound maximum cyclic iterations using `recursion_limit` (e.g. `graph.compile().invoke(..., {"recursion_limit": 25})`).
21- Model agent state explicitly: use `Annotated[list[BaseMessage], add_messages]` to ensure message append behavior without overwriting history.
22- Implement conditional routing edges (`add_conditional_edges`) checking tool call requests before executing external side effects.
23
24## 4. Structured Output & Tool Execution
25- Never parse raw LLM strings with regular expressions. Use `model.with_structured_output(PydanticSchema)` or native tool calling.
26- Sandbox tool execution with error boundaries: catch tool exceptions and return error messages back to the agent loop to allow self-correction.
27- Enforce timeout limits on external API calls executed by agents.
28
29## 5. Streaming & Observability
30- Expose streaming responses using Server-Sent Events (SSE) via FastAPI `StreamingResponse` using `graph.astream_events(version="v2")`.
31- Enable LangSmith or OpenTelemetry tracing via environment variables for complete inspection of prompt tokens, latency, and tool inputs.
32
33## 6. Testing Conventions
34- Unit test individual graph nodes as pure functions (given state, assert output state) before testing the compiled graph end-to-end.
35- Use LangSmith evaluation datasets or `pytest` fixtures with recorded LLM responses (cassettes) to keep tests deterministic and avoid burning API credits on every CI run.
36- Test tool-calling contracts by asserting the Pydantic schema rejects malformed arguments, not just that valid ones pass.
37- Assert `recursion_limit` is actually enforced with a test that forces a cyclic loop and expects a `GraphRecursionError`.
38
39## 7. Git Workflow & PR Conventions
40- Conventional Commits (`feat:`, `fix:`, `refactor:`) scoped to the agent/node, e.g. `fix(agents/retriever): handle empty vector store results`.
41- Any PR changing a prompt template or tool schema must include before/after LangSmith trace links or eval scores in the description.
42- Require `pytest`, `ruff check .`, and `mypy .` green before merge.
43- Flag prompt-only changes distinctly (`prompt:` commit prefix) so trace regressions are easy to bisect later.
ARCHITECTURE NOTES & IMPLEMENTATION GUIDE
Export as Markdown

Architecture Overview

Production architectural standard for autonomous agentic systems built on LangGraph, FastAPI, and Pydantic v2.

Key Advantages

  • Stateful Cyclic Workflows: LangGraph supports human-in-the-loop approvals, time-travel debugging, and cyclic reasoning loops where agents evaluate, revise, and retry failed actions.
  • Deterministic Tool Calling: Pydantic v2 schemas enforce strict input/output boundaries on tool execution, preventing hallucinated parameters.
  • Production Streaming: Async generators stream token-by-token thought processes and tool execution statuses directly to web clients.
FREQUENTLY ASKED QUESTIONS

Does this AGENTS.md work with Cursor, Claude Code, and Windsurf?

Yes — AGENTS.md is the open, cross-tool standard read by Cursor, Claude Code, Windsurf, and 30+ other agents. A dedicated .mdc file is also included for Cursor's native .cursor/rules format.

Why does an agent graph need an explicit recursion_limit?

LangGraph agents can enter cyclic reasoning loops by design — a tool call triggers another tool call, which triggers another, with no guaranteed termination condition. Without a recursion_limit, a single bad prompt or flaky tool response can spin an agent indefinitely, burning API credits and compute until it's killed manually.

Why avoid parsing LLM output with regular expressions?

LLM text output is inherently unstructured and varies between calls even with the same prompt, so regex patterns that work today will silently break on the next model update or an unusual completion. Native structured output or tool calling constrains the model to emit a schema-validated response, which fails loudly and predictably instead of silently mis-parsing.

MORE AI AGENT CODING RULES & SKILLS
View All Rules & Skills