Index of every page on this site: llms.txt
All skills
jeremylongshore avatar

/langchain-reference-architecture

@0d57af8
Run it yourself

A reference layered architecture for production LangChain 1.0 / LangGraph 1.0 services — LLM factory with version-safe defaults, chain/graph registry, retriever and tool DI, Pydantic-validated config, per-request tenant scoping, middleware ordering, checkpointer selection per environment. Use when starting a new service, refactoring a tangled chain, or onboarding a team to existing code. Trigger with "langchain architecture", "langchain llm factory", "langchain chain registry", "langchain dependency injection", "langchain project structure".

Use this Skill: https://skilld.dev/gh/jeremylongshore/tons-of-skills-marketplace/langchain-reference-architecture

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

referencesdirectory-layout.md

≈1.4k tokens on demand. Your agent reads this file only when SKILL.md points to it.

Directory Layout

The target tree for a LangChain 1.0 / LangGraph 1.0 service. Five layers, strict downward dependency, tests mirror the production tree.

Full Tree

my_service/
├── src/
│   └── my_service/
│       ├── __init__.py
│       │
│       ├── app/                              # Layer 1 — HTTP boundary
│       │   ├── __init__.py
│       │   ├── main.py                       # FastAPI() instance, lifespan, routers
│       │   ├── deps.py                       # FastAPI Depends() providers
│       │   ├── middleware.py                 # HTTP middleware (auth, request_id)
│       │   └── routes/
│       │       ├── __init__.py
│       │       ├── health.py                 # GET /healthz, /readyz
│       │       ├── support.py                # POST /support
│       │       └── triage.py                 # POST /triage
│       │
│       ├── services/                         # Layer 2 — chains and graphs
│       │   ├── __init__.py                   # imports each service to register it
│       │   ├── registry.py                   # @register / get(name, tenant=...)
│       │   ├── support/
│       │   │   ├── __init__.py
│       │   │   ├── chain.py                  # LCEL pipeline
│       │   │   └── graph.py                  # LangGraph StateGraph
│       │   └── triage/
│       │       ├── __init__.py
│       │       └── chain.py
│       │
│       ├── adapters/                         # Layer 3 — vendor integrations
│       │   ├── __init__.py
│       │   ├── llm_factory.py                # chat_model(provider, **kw)
│       │   ├── retriever_factory.py          # retriever_for(tenant_id=...)
│       │   ├── tool_factory.py               # tools_for(tenant_id=...)
│       │   ├── checkpointer.py               # checkpointer_for(env=...)
│       │   ├── history.py                    # history_for(session_id, tenant_id)
│       │   ├── middleware.py                 # LangChain middleware stack
│       │   └── telemetry.py                  # LangSmith / OTEL bootstrap
│       │
│       ├── config/                           # Layer 4 — configuration
│       │   ├── __init__.py
│       │   └── settings.py                   # Pydantic Settings class
│       │
│       └── domain/                           # Layer 5 — pure models
│           ├── __init__.py
│           ├── state.py                      # LangGraph TypedDict state
│           ├── models.py                     # request/response Pydantic models
│           └── enums.py
│
├── tests/
│   ├── unit/
│   │   ├── test_support_chain.py             # injects FakeListChatModel
│   │   └── test_retriever_factory.py         # asserts tenant isolation
│   ├── integration/
│   │   ├── test_postgres_checkpointer.py     # ephemeral PG
│   │   └── test_pinecone_namespace.py        # sandbox namespace
│   └── contract/
│       └── test_tool_schemas.py              # snapshots bind_tools JSON
│
├── .env.example                              # template; never commit real .env
├── pyproject.toml                            # [tool.importlinter] contracts here
├── README.md
└── Dockerfile

File-Naming Conventions

Pattern Meaning Example
*_factory.py Adapter that constructs a vendor-bound object per request or per env retriever_factory.py
*/chain.py LCEL (Runnable) composition services/support/chain.py
*/graph.py LangGraph StateGraph services/support/graph.py
registry.py Name → builder lookup inside a layer services/registry.py
settings.py Exactly one Pydantic BaseSettings subclass config/settings.py
state.py TypedDict or Pydantic model for LangGraph state domain/state.py
deps.py FastAPI Depends() providers only app/deps.py

Where Each SKILL Component Lives

Component from SKILL.md Step File
LLM factory (Step 2) adapters/llm_factory.py
Chain/graph registry (Step 3) services/registry.py
Retriever factory (Step 4) adapters/retriever_factory.py
Tool factory (Step 4) adapters/tool_factory.py
Pydantic Settings (Step 5) config/settings.py
Middleware stack (Step 6) adapters/middleware.py
Checkpointer selection (Step 7) adapters/checkpointer.py
Chat history selection (Step 7) adapters/history.py
Fakes and injection points (Step 8) tests/unit/conftest.py
import-linter contracts (Step 9) pyproject.toml

Depth Discipline

Typical production depth is 5 layers from route to domain model:

app.routes.support.run
  -> services.registry.get
    -> services.support.chain.build_support_agent
      -> adapters.llm_factory.chat_model
        -> langchain_anthropic.ChatAnthropic  # only here

If a stack trace shows a 7th frame inside langchain_* called from inside app/, the layer boundary was violated. Import-linter (Step 9 in SKILL.md) catches this at PR time, not at incident time.

Source: SKILL.md on GitHub

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

1 warning2mo3 checks · Risk SAFE
  • Gen Agent Trust Hub2mo

    The skill provides a production-ready reference architecture for LangChain and LangGraph applications, emphasizing security through strict layering, secret handling, and tenant isolation. It identifies an inherent indirect prompt injection attack surface that exists when LLM agents process untrusted external inputs via routes and retrievers.

  • Socket2mo

    No alerts

  • Snyk2mo

    Risk: MEDIUM · 1 issue

Signed by skilld at 0d57af8. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub yesterday.

Activeupdated 2 months ago
What it can do
Reads files Edits files
version
2.7.0
author
Jeremy Longshore <jeremy@intentsolutions.io>
compatibility
Designed for Claude Code
All 3 allowed tools
ReadWriteEdit
Other metadata
tags
[
  "saas",
  "langchain",
  "langgraph",
  "python",
  "langchain-1.0",
  "architecture",
  "reference-architecture",
  "patterns"
]

README badge

README badge for jeremylongshore/tons-of-skills-marketplace/langchain-reference-architecture