# pdichone/lang-production-api architecture

> Architecture diagram of the GitHub repository pdichone/lang-production-api, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-09-30). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/pdichone/lang-production-api
- Repository: https://github.com/pdichone/lang-production-api

## Overview

This repository implements a FastAPI chat service backed by a LangGraph agent. A client submits a chat request; the API validates its shape, sanitizes and masks input, checks the response cache, invokes the agent on a miss, validates the generated output, caches it, and returns a structured response. The service also exposes health, cache-statistics, and metrics endpoints, with rate limiting, structured logging, timing, and request metrics. The agent supports primary-model, fallback, and graceful-error paths. README content is empty, so capability coverage is based on the sampled source; external model and tracing links are shown only where source supports them.

Main entry points: [`app/main.py`](https://github.com/pdichone/lang-production-api/blob/main/app/main.py) (API routes), [`app/agent.py`](https://github.com/pdichone/lang-production-api/blob/main/app/agent.py) (LangGraph agent), [`app/security.py`](https://github.com/pdichone/lang-production-api/blob/main/app/security.py) (Security pipeline), [`app/models.py`](https://github.com/pdichone/lang-production-api/blob/main/app/models.py) (Request contracts)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["API"]
  node_api_routes["API routes<br/>[main.py]"]
  node_contracts["Request contracts<br/>[models.py]"]
  node_rate_limiter["Rate limiter<br/>[main.py]"]
end

subgraph group_processing["Chat processing"]
  node_security["Security pipeline<br/>[security.py]"]
  node_input_safety["Input checks<br/>[security.py]"]
  node_output_safety["Output checks<br/>[security.py]"]
  node_response_cache[("Response cache<br/>[cache.py]")]
  node_agent["LangGraph agent<br/>[agent.py]"]
end

subgraph group_operations["Operations"]
  node_settings["Runtime settings<br/>[config.py]"]
  node_logger["Structured logger<br/>[monitoring.py]"]
  node_metrics["Request metrics<br/>[monitoring.py]"]
  node_request_timer["Request timer<br/>[monitoring.py]"]
end

node_client(("Chat client"))
node_primary_model{{"Primary model"}}
node_fallback_model{{"Fallback model"}}
node_langsmith(("LangSmith tracing"))

node_client -->|"submits request"| node_api_routes
node_api_routes -->|"validates with"| node_contracts
node_api_routes -->|"applies limit"| node_rate_limiter
node_api_routes -->|"checks input"| node_security
node_security -->|"delegates input"| node_input_safety
node_security -->|"delegates output"| node_output_safety
node_api_routes -->|"looks up and stores"| node_response_cache
node_api_routes -->|"invokes on miss"| node_agent
node_agent -->|"loads settings"| node_settings
node_api_routes -->|"loads settings"| node_settings
node_agent -->|"invokes"| node_primary_model
node_agent -->|"invokes on failure"| node_fallback_model
node_api_routes -->|"returns response"| node_contracts
node_api_routes -->|"writes events"| node_logger
node_api_routes -->|"records requests"| node_metrics
node_api_routes -->|"times request"| node_request_timer
node_api_routes -.->|"traces endpoint"| node_langsmith
node_agent -.->|"traces invocation"| node_langsmith
node_security -.->|"traces checks"| node_langsmith

click node_api_routes "https://github.com/pdichone/lang-production-api/blob/main/app/main.py"
click node_contracts "https://github.com/pdichone/lang-production-api/blob/main/app/models.py"
click node_rate_limiter "https://github.com/pdichone/lang-production-api/blob/main/app/main.py"
click node_security "https://github.com/pdichone/lang-production-api/blob/main/app/security.py"
click node_input_safety "https://github.com/pdichone/lang-production-api/blob/main/app/security.py"
click node_output_safety "https://github.com/pdichone/lang-production-api/blob/main/app/security.py"
click node_response_cache "https://github.com/pdichone/lang-production-api/blob/main/app/cache.py"
click node_agent "https://github.com/pdichone/lang-production-api/blob/main/app/agent.py"
click node_settings "https://github.com/pdichone/lang-production-api/blob/main/app/config.py"
click node_logger "https://github.com/pdichone/lang-production-api/blob/main/app/monitoring.py"
click node_metrics "https://github.com/pdichone/lang-production-api/blob/main/app/monitoring.py"
click node_request_timer "https://github.com/pdichone/lang-production-api/blob/main/app/monitoring.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_api_routes,node_contracts,node_rate_limiter,node_client toneBlue
class node_security,node_input_safety,node_output_safety,node_response_cache,node_agent toneAmber
class node_settings,node_logger,node_metrics,node_request_timer toneMint
class node_primary_model,node_fallback_model,node_langsmith toneIndigo
```

## Components

### API

- [API routes](https://github.com/pdichone/lang-production-api/blob/main/app/main.py): `app/main.py`
- [Request contracts](https://github.com/pdichone/lang-production-api/blob/main/app/models.py): `app/models.py`
- [Rate limiter](https://github.com/pdichone/lang-production-api/blob/main/app/main.py): `app/main.py`

### Chat processing

- [Security pipeline](https://github.com/pdichone/lang-production-api/blob/main/app/security.py): `app/security.py`
- [Input checks](https://github.com/pdichone/lang-production-api/blob/main/app/security.py): `app/security.py`
- [Output checks](https://github.com/pdichone/lang-production-api/blob/main/app/security.py): `app/security.py`
- [Response cache](https://github.com/pdichone/lang-production-api/blob/main/app/cache.py): `app/cache.py`
- [LangGraph agent](https://github.com/pdichone/lang-production-api/blob/main/app/agent.py): `app/agent.py`

### Operations

- [Runtime settings](https://github.com/pdichone/lang-production-api/blob/main/app/config.py): `app/config.py`
- [Structured logger](https://github.com/pdichone/lang-production-api/blob/main/app/monitoring.py): `app/monitoring.py`
- [Request metrics](https://github.com/pdichone/lang-production-api/blob/main/app/monitoring.py): `app/monitoring.py`
- [Request timer](https://github.com/pdichone/lang-production-api/blob/main/app/monitoring.py): `app/monitoring.py`

### Other components

- **Chat client**
- **Primary model**
- **Fallback model**
- **LangSmith tracing**

## Connections

- Chat client → API routes: submits request (evidence: `app/main.py`)
- API routes → Request contracts: validates with (evidence: `app/main.py`)
- API routes → Rate limiter: applies limit (evidence: `app/main.py`)
- API routes → Security pipeline: checks input (evidence: `app/main.py`)
- Security pipeline → Input checks: delegates input (evidence: `app/security.py`)
- Security pipeline → Output checks: delegates output (evidence: `app/security.py`)
- API routes → Response cache: looks up and stores (evidence: `app/main.py`)
- API routes → LangGraph agent: invokes on miss (evidence: `app/main.py`)
- LangGraph agent → Runtime settings: loads settings (evidence: `app/agent.py`)
- API routes → Runtime settings: loads settings (evidence: `app/main.py`)
- LangGraph agent → Primary model: invokes (evidence: `app/agent.py`)
- LangGraph agent → Fallback model: invokes on failure (evidence: `app/agent.py`)
- API routes → Request contracts: returns response (evidence: `app/main.py`)
- API routes → Structured logger: writes events (evidence: `app/main.py`)
- API routes → Request metrics: records requests (evidence: `app/main.py`)
- API routes → Request timer: times request (evidence: `app/main.py`)
- API routes → LangSmith tracing: traces endpoint (evidence: `app/main.py`)
- LangGraph agent → LangSmith tracing: traces invocation (evidence: `app/agent.py`)
- Security pipeline → LangSmith tracing: traces checks (evidence: `app/security.py`)
