Why I Replaced LangGraph with 200 Lines of Direct TypeScript on Scale-to-Zero Cloud Run
Applied AI · 7 min
Agent frameworks shine for rapid prototyping, but on scale-to-zero serverless containers their dependency weight, cold-start cost, and opaque state serialization can outweigh their benefits. Here is the 200-line TypeScript state machine that replaced LangGraph.
When I first built the stateful Architecture Copilot demo (ADR-024)—where a Cloud Architect agent drafts an initial topology, DevSecOps and FinOps specialist agents audit it in parallel, and execution pauses at a human-in-the-loop approval gate before mutating the graph—I built it on @langchain/langgraph and @langchain/core.
Within a month of running in production on scale-to-zero Cloud Run, the trade-offs became painful:
- Cold-start and bundle weight: Even with lazy dynamic imports, pulling in
@langchain/langgraphand@langchain/coreinflatednode_modulesand container layer extraction time. - Abstraction friction for a fixed topology: My workflow has a well-defined, bounded topology (
draft→ parallelsecurityAudit+finOpsAudit→checkpoint & pause→ deterministicresume). Expressing a 4-stage pipeline through graph annotations, channels, and customBaseCheckpointSaveradapters required more boilerplate than the actual domain logic. - Opaque checkpoint documents: Persisting LangGraph's internal channel tuple structures in Firestore made checkpoints harder to inspect, version, and validate with strict TypeScript types.
The 200-Line Direct TypeScript Replacement (`server/ai/diagramAgent.ts`)
In November 2026 (ADR-024 amendment), I deleted @langchain/langgraph and @langchain/core and replaced the entire orchestration layer with direct TypeScript functions backed by my two-tier L1/L2 Firestore cache (server/cache.ts).
1. Parallel Specialist Execution with `Promise.all` + `Promise.race` Deadlines
Instead of a framework scheduler, the initial design and parallel specialist audits are plain async functions using the official @google/genai SDK with schema-constrained JSON output:
// Step 1: Cloud Architect drafts the initial node/link graph
const baseTopology = await runArchitectAgent(client, activeModel, description);
// Step 2: DevSecOps Auditor and FinOps Consultant critique the topology in parallel,
// bounded by a 15-second per-audit deadline so a slow secondary call never hangs the UX
const [secSuggestions, costSuggestions] = await Promise.all([
withAuditTimeout(runSecurityAuditor(client, activeModel, description, baseTopology), []),
withAuditTimeout(runFinOpsConsultant(client, activeModel, description, baseTopology), []),
]);
```
Notice the graceful degradation in withAuditTimeout: if one specialist audit times out on an upstream Vertex AI spike, the user still gets the base diagram and the surviving auditor's findings rather than a 503 failure.
2. Transparent, Typed Firestore Checkpoints
When the auditors return recommendations, the orchestrator writes a clean, human-readable CopilotSession document to L1 memory and Firestore's checkpoints collection with a 7-day TTL policy:
export interface CopilotSession {
threadId: string;
nodes: DiagramNode[];
links: DiagramLink[];
suggestions: Recommendation[];
model: string;
createdAt: number;
expiresAt: number;
}
3. Zero-LLM Deterministic Resume
When the visitor reviews the DevSecOps and FinOps cards in the UI, checks the optimizations they want, and clicks Apply Selected, POST /api/ai/copilot/resume loads the checkpoint from Firestore (even if the original Cloud Run instance scaled to zero or the request landed on a different replica) and applies the approved graph mutations deterministically in pure TypeScript—consuming zero LLM tokens and zero daily AI budget.