[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"skill:01M792ABWZNCRSQA2RZP3RAK7T":3,"skill-same:01M792ABWZNCRSQA2RZP3RAK7T":62},{"skillNo":4,"name":5,"nameZh":6,"description":7,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":16,"categories":17,"keywords":18,"publishedAt":19,"linkOnly":20,"markdownContent":21,"markdownBody":22,"frontMatter":23,"refName":39,"commitSha":40,"skillDirectory":41,"forksCount":42,"repositoryHtmlUrl":43,"repositoryHomepageUrl":6,"ownerHtmlUrl":44,"ownerBlogUrl":45,"ownerDescription":46,"updatedAt":47,"install":48,"resources":61},"01M792ABWZNCRSQA2RZP3RAK7T","langchain-deploy-integration",null,"Deploy a LangChain 1.0 \u002F LangGraph 1.0 app to Cloud Run, Vercel, or LangServe\ncorrectly — timeouts sized for chain length, cold-start mitigation, SSE\nanti-buffering headers, Secret Manager over `.env`. Use when prepping first\nprod deploy, debugging a stream that hangs behind a proxy, or diagnosing p99\nlatency spikes.\nTrigger with \"langchain deploy\", \"langchain cloud run\", \"langchain vercel python\",\n\"langchain langserve\", \"langchain docker\".","jukeyman\u002Fjukeyman-skills","jukeyman","https:\u002F\u002Favatars.githubusercontent.com\u002Fu\u002F142960420?v=4","01M78FR4Y3DHMMCBCC0GXXNB3F","RICK JEFFERSON","01M78FR4Z8RVQRAD31Y5J7V6EB",1,"MIT","https:\u002F\u002Fgithub.com\u002Fjukeyman\u002Fjukeyman-skills\u002Fblob\u002Fmain\u002Fskills\u002Fsaas-packs--langchain-py-pack--langchain-deploy-integration\u002FSKILL.md",[],[],"2026-11-15 14:19:17",false,"---\nname: langchain-deploy-integration\ndescription: |\n  Deploy a LangChain 1.0 \u002F LangGraph 1.0 app to Cloud Run, Vercel, or LangServe\n  correctly — timeouts sized for chain length, cold-start mitigation, SSE\n  anti-buffering headers, Secret Manager over `.env`. Use when prepping first\n  prod deploy, debugging a stream that hangs behind a proxy, or diagnosing p99\n  latency spikes.\n  Trigger with \"langchain deploy\", \"langchain cloud run\", \"langchain vercel python\",\n  \"langchain langserve\", \"langchain docker\".\nallowed-tools: Read, Write, Edit, Bash(docker:*), Bash(gcloud:*), Bash(vercel:*)\nversion: 2.0.0\nlicense: MIT\nauthor: Jeremy Longshore \u003Cjeremy@intentsolutions.io>\ntags: [saas, langchain, langgraph, python, langchain-1.0, deployment, cloud-run, vercel, langserve]\ncompatible-with: claude-code, codex\n---\n\n# LangChain Deploy Integration (Python)\n\n## Overview\n\nAn engineer ships a working LangGraph agent to Vercel. Every non-trivial request\nreturns `FUNCTION_INVOCATION_TIMEOUT`. The Python runtime on Vercel defaults to\na **10-second** cap (P35) — a three-tool agent with one RAG round easily runs\n20-40s. Local dev never exposed the wall because `uvicorn` on a laptop has no\ntimeout. Two fixes apply together and each is load-bearing:\n\n```json\n\u002F\u002F vercel.json — the baseline cap bump (Pro plan max is 60s, Enterprise 900s)\n{ \"functions\": { \"api\u002Fchat.py\": { \"maxDuration\": 60 } } }\n```\n\n```python\n# app\u002Fapi\u002Fchat.py — stream the response so partial output arrives before the cap\nfrom fastapi.responses import StreamingResponse\n\n@app.post(\"\u002Fapi\u002Fchat\")\nasync def chat(req: ChatRequest):\n    async def gen():\n        async for chunk in chain.astream(req.input):\n            yield f\"data: {chunk.model_dump_json()}\\n\\n\"\n    return StreamingResponse(gen(), media_type=\"text\u002Fevent-stream\",\n                             headers={\"X-Accel-Buffering\": \"no\"})\n```\n\nThe `maxDuration: 60` raises the Vercel-imposed wall; streaming reduces\ntime-to-first-byte to under a second so the user sees progress even on a\n40-second completion. Once the Vercel cap is fixed, the next three walls are:\nCloud Run cold starts (**5-15s** p99 on Python + LangChain — P36), `.env`\nsecrets leaking via `docker exec \u003Cpod> env` (P37), and SSE streams hanging\nbecause Nginx \u002F Cloud Run buffer the final chunk (P46).\n\nThis skill walks through a production-grade multi-stage Dockerfile, Cloud Run\nflags for cold-start mitigation, Vercel `maxDuration` + streaming, LangServe\nroute mounting with FastAPI lifespan, SSE anti-buffering headers, and Secret\nManager via `pydantic.SecretStr`. Pin: `langchain-core 1.0.x`, `langgraph 1.0.x`,\n`langserve 1.0.x`. Pain-catalog anchors: **P35** (Vercel 10s default),\n**P36** (Cloud Run cold start), **P37** (`.env` leaks), **P46** (SSE buffering).\n\n## Prerequisites\n\n- Python 3.11+ (3.12 preferred for `uvicorn` startup speed)\n- `langchain-core >= 1.0, \u003C 2.0`, `langgraph >= 1.0, \u003C 2.0`, `langserve >= 1.0, \u003C 2.0`\n- `fastapi >= 0.110`, `uvicorn[standard] >= 0.27`\n- Target platform: `gcloud` CLI (Cloud Run), `vercel` CLI (Vercel), or `docker` (generic)\n- For Cloud Run: a GCP project with Secret Manager API enabled\n- For Vercel: a project with `@vercel\u002Fpython` runtime configured\n\n## Instructions\n\n### Step 1 — Multi-stage Dockerfile with slim runtime and `uvicorn`\n\nA multi-stage build keeps the runtime image under 400MB, which cuts Cloud Run\ncold starts by 2-3 seconds. Use `python:3.12-slim` as the final stage (not\n`python:3.12` — that base adds ~900MB for dev tooling that never runs in prod).\n\n```dockerfile\n# syntax=docker\u002Fdockerfile:1.7\nFROM python:3.12-slim AS builder\nWORKDIR \u002Fbuild\nRUN pip install --no-cache-dir uv\nCOPY pyproject.toml uv.lock .\u002F\nRUN uv export --format requirements-txt --no-hashes > requirements.txt \\\n && pip wheel --wheel-dir=\u002Fwheels -r requirements.txt\n\nFROM python:3.12-slim AS runtime\nRUN useradd -m -u 10001 app\nWORKDIR \u002Fapp\nCOPY --from=builder \u002Fwheels \u002Fwheels\nRUN pip install --no-cache-dir --no-index --find-links=\u002Fwheels \u002Fwheels\u002F* \\\n && rm -rf \u002Fwheels\nCOPY --chown=app:app app\u002F .\u002Fapp\u002F\nUSER app\nEXPOSE 8080\nENV PORT=8080 PYTHONUNBUFFERED=1 PYTHONDONTWRITEBYTECODE=1\nCMD [\"sh\", \"-c\", \"uvicorn app.main:app --host 0.0.0.0 --port ${PORT} --workers 1\"]\n```\n\nSingle worker is correct — Cloud Run handles horizontal scale; in-process\nmulti-worker just duplicates LangChain client memory. See [Dockerfile and\nSecrets](references\u002Fdockerfile-and-secrets.md) for the distroless variant\nand the `.dockerignore` hardening for `.env` files.\n\n### Step 2 — Deploy to Cloud Run with cold-start mitigation\n\nPython + LangChain + `tiktoken` + one embedding model imports take 5-15\nseconds (P36). At `--min-instances=0`, every scale-from-zero request eats that\nas user-facing latency. Paying for one always-on instance is usually cheaper\nthan the lost requests.\n\n```bash\ngcloud run deploy langchain-api \\\n  --source=. \\\n  --region=us-central1 \\\n  --min-instances=1 \\\n  --max-instances=20 \\\n  --cpu=2 --memory=2Gi \\\n  --cpu-boost \\\n  --no-cpu-throttling \\\n  --timeout=3600 \\\n  --concurrency=80 \\\n  --set-secrets=ANTHROPIC_API_KEY=anthropic-key:latest,OPENAI_API_KEY=openai-key:latest \\\n  --service-account=langchain-api@PROJECT.iam.gserviceaccount.com\n# --timeout=3600 is the Cloud Run per-request maximum (1 hour) — needed\n# because multi-tool LangGraph agents routinely run 1-5 minutes end-to-end.\n```\n\nThe load-bearing flags: `--min-instances=1` kills cold-start p99 (one always-warm\nreplica costs ~$15\u002Fmo and dominates p99 improvement); `--cpu-boost` doubles CPU\nfor the first 10 seconds; `--no-cpu-throttling` (CPU-always-allocated billing)\nkeeps `astream` running between keepalive pings so long LangGraph runs do not\nstall at tool boundaries; `--concurrency=80` matches typical I\u002FO-bound\nworkloads (drop to 10 if embedding large docs in-process).\n\nSee [Cloud Run Deploy](references\u002Fcloud-run-deploy.md) for VPC egress, file\nsecret mounts, revision traffic splitting, and the full cost model.\n\n### Step 3 — Vercel Python: `maxDuration: 60` + streaming to beat the cap\n\nOn Vercel Hobby the max is **10s** by default (P35); Pro is **60s**, Enterprise\n**900s**. Always set `maxDuration` explicitly — the default is a trap.\n\n```json\n\u002F\u002F vercel.json\n{\n  \"functions\": {\n    \"api\u002Fchat.py\": { \"maxDuration\": 60, \"memory\": 1024 }\n  }\n}\n```\n\nStreaming is not just a UX fix — it is the mitigation for bursts that still\nexceed `maxDuration`. Time-to-first-byte under a second keeps the proxy\nconsidering the request alive; partial content renders on the client; when\nthe cap finally triggers, the user has already seen most of the answer. The\nVercel entrypoint pattern mirrors the Overview snippet above — pair with the\nSSE headers from Step 5.\n\nEdge Runtime is **not** an option here — `@vercel\u002Fedge` is JavaScript-only.\nAnything that imports `langchain` must run on `@vercel\u002Fpython` (serverless,\nNode-free Python container). See [Vercel Python Deploy](references\u002Fvercel-python-deploy.md)\nfor env vars vs Vercel Secrets, cold-start profiling, and the serverless vs\nfluid-compute tradeoff.\n\n### Step 4 — LangServe: `add_routes` + FastAPI lifespan for pool cleanup\n\nLangServe ships typed HTTP routes over any `Runnable`. The `playground` path\nis invaluable in dev but **must be disabled in production** — it leaks chain\ntopology to anyone who can hit the URL. Mount behind a FastAPI `lifespan`\nthat closes `asyncpg` \u002F `httpx` \u002F Redis pools on revision retirement;\n`on_shutdown` fires too late on Cloud Run and connections leak across\nrevisions.\n\n```python\n# app\u002Fmain.py\nfrom contextlib import asynccontextmanager\nfrom fastapi import FastAPI\nfrom langserve import add_routes\n\n@asynccontextmanager\nasync def lifespan(app: FastAPI):\n    app.state.chain = build_chain()\n    yield\n    await db_pool.close()\n\napp = FastAPI(lifespan=lifespan)\nadd_routes(app, build_chain(), path=\"\u002Fchat\",\n           enable_feedback_endpoint=False,\n           playground_type=\"chat\" if __debug__ else None)  # None = off in prod\n```\n\nSee [LangServe Patterns](references\u002Flangserve-patterns.md) for typed input\u002Foutput\nschemas, auth middleware, and coexisting with raw FastAPI handlers.\n\n### Step 5 — SSE anti-buffering: survive Nginx, Cloud Run, Cloudflare\n\nNginx, Cloud Run's load balancer, and Cloudflare all buffer responses by\ndefault. On SSE, buffering means the client never sees the final `end` event\nand `LangGraph.astream` hangs forever (P46). Two headers plus one response\nflush fix it:\n\n```python\nfrom fastapi.responses import StreamingResponse\n\ndef sse_headers() -> dict:\n    return {\n        \"Content-Type\": \"text\u002Fevent-stream\",\n        \"Cache-Control\": \"no-cache, no-transform\",\n        \"X-Accel-Buffering\": \"no\",          # disables Nginx buffering\n        \"Connection\": \"keep-alive\",\n    }\n\n@app.post(\"\u002Fapi\u002Fchat\u002Fstream\")\nasync def stream(payload: dict):\n    async def gen():\n        async for event in graph.astream_events(payload, version=\"v2\"):\n            yield f\"data: {event['data']}\\n\\n\".encode(\"utf-8\")\n        yield b\"event: end\\ndata: [DONE]\\n\\n\"\n    return StreamingResponse(gen(), headers=sse_headers())\n```\n\nCross-reference: this is the same anti-buffering pattern used by\n`langchain-langgraph-streaming` (L29) — that skill covers the `astream_events`\nevent shapes and client reconnection; this skill covers the proxy surface.\nIf you see the stream work locally but hang in prod, the header is missing on\nthe upstream response, not the client.\n\n### Step 6 — Secret Manager over `.env` with `pydantic.SecretStr`\n\n`python-dotenv` populates `os.environ`. Anyone with container access runs\n`docker exec \u003Cpod> env` and reads every API key in plain text (P37). Mount\nsecrets from Secret Manager on Cloud Run, Vercel Secrets on Vercel; wrap in\n`pydantic.SecretStr` so `repr()`, logs, and tracebacks print `**********`\ninstead of the value.\n\n```python\nfrom pydantic import SecretStr\nfrom pydantic_settings import BaseSettings, SettingsConfigDict\n\nclass Settings(BaseSettings):\n    model_config = SettingsConfigDict(env_file=None, extra=\"ignore\")\n    anthropic_api_key: SecretStr\n    openai_api_key: SecretStr\n\nsettings = Settings()  # reads real env vars only; never .env in prod\n```\n\nCloud Run: `--set-secrets=VAR=secret-name:latest` (Step 2). Vercel:\n`vercel env add ANTHROPIC_API_KEY production`. Never commit `.env`; add\n`.env*` to `.dockerignore` so it does not enter the build context.\n\nCross-reference: this skill sets the deployment boundary. `langchain-security-basics`\n(P18) covers key-rotation cadence, PII log redaction, and the broader threat\nmodel — get the boundary right here, then layer on P18.\n\n## Output\n\n- Multi-stage Dockerfile, runtime image under 400MB, non-root user, `uvicorn` entrypoint\n- Cloud Run deployment with `--min-instances=1 --cpu-boost --no-cpu-throttling --timeout=3600 --concurrency=80`\n- `vercel.json` with `maxDuration: 60` plus streaming response for requests that may exceed the cap\n- LangServe `add_routes(app, chain, path=\"\u002Fchat\")` behind a FastAPI `lifespan` that closes resource pools on revision retirement\n- SSE responses with `X-Accel-Buffering: no` and `Cache-Control: no-cache` so proxies do not buffer the final `end` event\n- Runtime secrets sourced from Secret Manager \u002F Vercel Secrets and wrapped in `pydantic.SecretStr`; no `.env` in the image\n\n## Platform Decision Table\n\n| Platform | Max timeout | Cold start (p99) | SSE default | Cost baseline (1 agent, 1M req\u002Fmo) | Pick when |\n|----------|-------------|------------------|-------------|------------------------------------|-----------|\n| **Cloud Run** | 3600s | 5-15s (mitigable with min-instances) | Buffers without header override | ~$40-80\u002Fmo + min-instance | Long agents, heavy imports, VPC egress, GCP-native |\n| **Vercel Python** | 10s Hobby \u002F 60s Pro \u002F 900s Enterprise | 2-5s (warmed) | No buffering when streaming response | ~$20\u002Fmo Pro plan flat | Fast iteration, Next.js frontend colocation, short agents |\n| **Fly.io** | No hard cap (per-request soft) | 1-3s (always-on VM) | No default buffer | ~$30\u002Fmo for shared-cpu-1x + 1GB | Need persistent state, low cold start, non-serverless model |\n| **Railway** | 30 min default | 2-4s | No default buffer | ~$25\u002Fmo on Hobby replica | Prototype to production on one platform, Postgres bundled |\n| **Self-hosted (k8s + Nginx)** | Ingress-controlled | 0s (warm pod) | Buffers — requires `proxy_buffering off` | Variable, fixed infra cost | Compliance constraints, existing k8s, egress control |\n\nCloud Run is the default for most LangChain production deployments — the 3600s\ntimeout is the only mainstream serverless option long enough for multi-tool\nagents, and Secret Manager integration is native.\n\n## Error Handling\n\n| Error | Cause | Fix |\n|-------|-------|-----|\n| `FUNCTION_INVOCATION_TIMEOUT` (Vercel) | 10s Python default on Hobby, 60s on Pro (P35) | Set `maxDuration: 60` in `vercel.json`; stream the response |\n| `504 Gateway Timeout` from Cloud Run | Missing `--timeout=3600`; default is 300s | Redeploy with `--timeout=3600 --concurrency=80` |\n| p99 latency 10x p95 | Cold start from Python + LangChain imports (P36) | `--min-instances=1 --cpu-boost`, defer `tiktoken` import to request time |\n| Client hangs forever on SSE stream | Proxy buffering the final `end` event (P46) | Add `X-Accel-Buffering: no` and `Cache-Control: no-cache` headers |\n| `docker exec \u003Cpod> env` shows API keys | `python-dotenv` leaked `.env` into `os.environ` (P37) | Delete `.env` from image, use Secret Manager + `pydantic.SecretStr` |\n| `RuntimeError: Event loop is closed` on shutdown | `asyncpg` \u002F `httpx` pool not closed before revision retirement | Move pool lifecycle into FastAPI `lifespan` context manager |\n| Stream works locally, stalls on Cloud Run | CPU throttling between keepalive pings | Deploy with `--no-cpu-throttling` (CPU-always-allocated) |\n| `ModuleNotFoundError` in Vercel build | `requirements.txt` not generated from `pyproject.toml` | Add build step: `uv export --format requirements-txt > requirements.txt` |\n\n## Examples\n\n### A: Cloud Run with min-instances, secret mounts, VPC egress\n\nOne always-warm replica, two secrets from Secret Manager, egress routed\nthrough a VPC connector for private Postgres. See [Cloud Run Deploy](references\u002Fcloud-run-deploy.md)\nfor the full flag set, revision traffic splitting, and the cost model.\n\n### B: Vercel with streaming and maxDuration\n\n`vercel.json` with `maxDuration: 60` plus the FastAPI streaming entrypoint\nfrom Step 3. The combination gives a 40-second completion a 60-second wall\nand \u003C1s time-to-first-byte. See [Vercel Python Deploy](references\u002Fvercel-python-deploy.md)\nfor Edge Runtime limits and the Vercel Secrets vs env var split.\n\n### C: LangServe behind FastAPI with lifespan\n\nSingle `add_routes` call mounting `\u002Fchat` and `\u002Fchat\u002Fstream` with a shared\nconnection pool closed via `lifespan`. Playground disabled in prod via the\n`__debug__` check. See [LangServe Patterns](references\u002Flangserve-patterns.md)\nfor typed schemas, auth middleware, and raw-handler coexistence.\n\n## Resources\n\n- [Cloud Run: Configuring services](https:\u002F\u002Fcloud.google.com\u002Frun\u002Fdocs\u002Fconfiguring\u002Fservices)\n- [Cloud Run: Always-on CPU (no CPU throttling)](https:\u002F\u002Fcloud.google.com\u002Frun\u002Fdocs\u002Fconfiguring\u002Fcpu-allocation)\n- [Vercel: Python runtime](https:\u002F\u002Fvercel.com\u002Fdocs\u002Ffunctions\u002Fruntimes\u002Fpython)\n- [Vercel: `maxDuration` and streaming](https:\u002F\u002Fvercel.com\u002Fdocs\u002Ffunctions\u002Fconfiguring-functions\u002Fduration)\n- [LangServe: Getting started](https:\u002F\u002Fpython.langchain.com\u002Fdocs\u002Flangserve\u002F)\n- [FastAPI: Lifespan events](https:\u002F\u002Ffastapi.tiangolo.com\u002Fadvanced\u002Fevents\u002F)\n- [Pydantic: `SecretStr`](https:\u002F\u002Fdocs.pydantic.dev\u002Flatest\u002Fapi\u002Ftypes\u002F#pydantic.types.SecretStr)\n- Pack pain catalog: `docs\u002Fpain-catalog.md` (entries P35, P36, P37, P46)\n- Related skills: `langchain-langgraph-streaming` (L29), `langchain-security-basics` (P18)\n","# LangChain Deploy Integration (Python)\n\n## Overview\n\nAn engineer ships a working LangGraph agent to Vercel. Every non-trivial request\nreturns `FUNCTION_INVOCATION_TIMEOUT`. The Python runtime on Vercel defaults to\na **10-second** cap (P35) — a three-tool agent with one RAG round easily runs\n20-40s. Local dev never exposed the wall because `uvicorn` on a laptop has no\ntimeout. Two fixes apply together and each is load-bearing:\n\n```json\n\u002F\u002F vercel.json — the baseline cap bump (Pro plan max is 60s, Enterprise 900s)\n{ \"functions\": { \"api\u002Fchat.py\": { \"maxDuration\": 60 } } }\n```\n\n```python\n# app\u002Fapi\u002Fchat.py — stream the response so partial output arrives before the cap\nfrom fastapi.responses import StreamingResponse\n\n@app.post(\"\u002Fapi\u002Fchat\")\nasync def chat(req: ChatRequest):\n    async def gen():\n        async for chunk in chain.astream(req.input):\n            yield f\"data: {chunk.model_dump_json()}\\n\\n\"\n    return StreamingResponse(gen(), media_type=\"text\u002Fevent-stream\",\n                             headers={\"X-Accel-Buffering\": \"no\"})\n```\n\nThe `maxDuration: 60` raises the Vercel-imposed wall; streaming reduces\ntime-to-first-byte to under a second so the user sees progress even on a\n40-second completion. Once the Vercel cap is fixed, the next three walls are:\nCloud Run cold starts (**5-15s** p99 on Python + LangChain — P36), `.env`\nsecrets leaking via `docker exec \u003Cpod> env` (P37), and SSE streams hanging\nbecause Nginx \u002F Cloud Run buffer the final chunk (P46).\n\nThis skill walks through a production-grade multi-stage Dockerfile, Cloud Run\nflags for cold-start mitigation, Vercel `maxDuration` + streaming, LangServe\nroute mounting with FastAPI lifespan, SSE anti-buffering headers, and Secret\nManager via `pydantic.SecretStr`. Pin: `langchain-core 1.0.x`, `langgraph 1.0.x`,\n`langserve 1.0.x`. Pain-catalog anchors: **P35** (Vercel 10s default),\n**P36** (Cloud Run cold start), **P37** (`.env` leaks), **P46** (SSE buffering).\n\n## Prerequisites\n\n- Python 3.11+ (3.12 preferred for `uvicorn` startup speed)\n- `langchain-core >= 1.0, \u003C 2.0`, `langgraph >= 1.0, \u003C 2.0`, `langserve >= 1.0, \u003C 2.0`\n- `fastapi >= 0.110`, `uvicorn[standard] >= 0.27`\n- Target platform: `gcloud` CLI (Cloud Run), `vercel` CLI (Vercel), or `docker` (generic)\n- For Cloud Run: a GCP project with Secret Manager API enabled\n- For Vercel: a project with `@vercel\u002Fpython` runtime configured\n\n## Instructions\n\n### Step 1 — Multi-stage Dockerfile with slim runtime and `uvicorn`\n\nA multi-stage build keeps the runtime image under 400MB, which cuts Cloud Run\ncold starts by 2-3 seconds. Use `python:3.12-slim` as the final stage (not\n`python:3.12` — that base adds ~900MB for dev tooling that never runs in prod).\n\n```dockerfile\n# syntax=docker\u002Fdockerfile:1.7\nFROM python:3.12-slim AS builder\nWORKDIR \u002Fbuild\nRUN pip install --no-cache-dir uv\nCOPY pyproject.toml uv.lock .\u002F\nRUN uv export --format requirements-txt --no-hashes > requirements.txt \\\n && pip wheel --wheel-dir=\u002Fwheels -r requirements.txt\n\nFROM python:3.12-slim AS runtime\nRUN useradd -m -u 10001 app\nWORKDIR \u002Fapp\nCOPY --from=builder \u002Fwheels \u002Fwheels\nRUN pip install --no-cache-dir --no-index --find-links=\u002Fwheels \u002Fwheels\u002F* \\\n && rm -rf \u002Fwheels\nCOPY --chown=app:app app\u002F .\u002Fapp\u002F\nUSER app\nEXPOSE 8080\nENV PORT=8080 PYTHONUNBUFFERED=1 PYTHONDONTWRITEBYTECODE=1\nCMD [\"sh\", \"-c\", \"uvicorn app.main:app --host 0.0.0.0 --port ${PORT} --workers 1\"]\n```\n\nSingle worker is correct — Cloud Run handles horizontal scale; in-process\nmulti-worker just duplicates LangChain client memory. See [Dockerfile and\nSecrets](references\u002Fdockerfile-and-secrets.md) for the distroless variant\nand the `.dockerignore` hardening for `.env` files.\n\n### Step 2 — Deploy to Cloud Run with cold-start mitigation\n\nPython + LangChain + `tiktoken` + one embedding model imports take 5-15\nseconds (P36). At `--min-instances=0`, every scale-from-zero request eats that\nas user-facing latency. Paying for one always-on instance is usually cheaper\nthan the lost requests.\n\n```bash\ngcloud run deploy langchain-api \\\n  --source=. \\\n  --region=us-central1 \\\n  --min-instances=1 \\\n  --max-instances=20 \\\n  --cpu=2 --memory=2Gi \\\n  --cpu-boost \\\n  --no-cpu-throttling \\\n  --timeout=3600 \\\n  --concurrency=80 \\\n  --set-secrets=ANTHROPIC_API_KEY=anthropic-key:latest,OPENAI_API_KEY=openai-key:latest \\\n  --service-account=langchain-api@PROJECT.iam.gserviceaccount.com\n# --timeout=3600 is the Cloud Run per-request maximum (1 hour) — needed\n# because multi-tool LangGraph agents routinely run 1-5 minutes end-to-end.\n```\n\nThe load-bearing flags: `--min-instances=1` kills cold-start p99 (one always-warm\nreplica costs ~$15\u002Fmo and dominates p99 improvement); `--cpu-boost` doubles CPU\nfor the first 10 seconds; `--no-cpu-throttling` (CPU-always-allocated billing)\nkeeps `astream` running between keepalive pings so long LangGraph runs do not\nstall at tool boundaries; `--concurrency=80` matches typical I\u002FO-bound\nworkloads (drop to 10 if embedding large docs in-process).\n\nSee [Cloud Run Deploy](references\u002Fcloud-run-deploy.md) for VPC egress, file\nsecret mounts, revision traffic splitting, and the full cost model.\n\n### Step 3 — Vercel Python: `maxDuration: 60` + streaming to beat the cap\n\nOn Vercel Hobby the max is **10s** by default (P35); Pro is **60s**, Enterprise\n**900s**. Always set `maxDuration` explicitly — the default is a trap.\n\n```json\n\u002F\u002F vercel.json\n{\n  \"functions\": {\n    \"api\u002Fchat.py\": { \"maxDuration\": 60, \"memory\": 1024 }\n  }\n}\n```\n\nStreaming is not just a UX fix — it is the mitigation for bursts that still\nexceed `maxDuration`. Time-to-first-byte under a second keeps the proxy\nconsidering the request alive; partial content renders on the client; when\nthe cap finally triggers, the user has already seen most of the answer. The\nVercel entrypoint pattern mirrors the Overview snippet above — pair with the\nSSE headers from Step 5.\n\nEdge Runtime is **not** an option here — `@vercel\u002Fedge` is JavaScript-only.\nAnything that imports `langchain` must run on `@vercel\u002Fpython` (serverless,\nNode-free Python container). See [Vercel Python Deploy](references\u002Fvercel-python-deploy.md)\nfor env vars vs Vercel Secrets, cold-start profiling, and the serverless vs\nfluid-compute tradeoff.\n\n### Step 4 — LangServe: `add_routes` + FastAPI lifespan for pool cleanup\n\nLangServe ships typed HTTP routes over any `Runnable`. The `playground` path\nis invaluable in dev but **must be disabled in production** — it leaks chain\ntopology to anyone who can hit the URL. Mount behind a FastAPI `lifespan`\nthat closes `asyncpg` \u002F `httpx` \u002F Redis pools on revision retirement;\n`on_shutdown` fires too late on Cloud Run and connections leak across\nrevisions.\n\n```python\n# app\u002Fmain.py\nfrom contextlib import asynccontextmanager\nfrom fastapi import FastAPI\nfrom langserve import add_routes\n\n@asynccontextmanager\nasync def lifespan(app: FastAPI):\n    app.state.chain = build_chain()\n    yield\n    await db_pool.close()\n\napp = FastAPI(lifespan=lifespan)\nadd_routes(app, build_chain(), path=\"\u002Fchat\",\n           enable_feedback_endpoint=False,\n           playground_type=\"chat\" if __debug__ else None)  # None = off in prod\n```\n\nSee [LangServe Patterns](references\u002Flangserve-patterns.md) for typed input\u002Foutput\nschemas, auth middleware, and coexisting with raw FastAPI handlers.\n\n### Step 5 — SSE anti-buffering: survive Nginx, Cloud Run, Cloudflare\n\nNginx, Cloud Run's load balancer, and Cloudflare all buffer responses by\ndefault. On SSE, buffering means the client never sees the final `end` event\nand `LangGraph.astream` hangs forever (P46). Two headers plus one response\nflush fix it:\n\n```python\nfrom fastapi.responses import StreamingResponse\n\ndef sse_headers() -> dict:\n    return {\n        \"Content-Type\": \"text\u002Fevent-stream\",\n        \"Cache-Control\": \"no-cache, no-transform\",\n        \"X-Accel-Buffering\": \"no\",          # disables Nginx buffering\n        \"Connection\": \"keep-alive\",\n    }\n\n@app.post(\"\u002Fapi\u002Fchat\u002Fstream\")\nasync def stream(payload: dict):\n    async def gen():\n        async for event in graph.astream_events(payload, version=\"v2\"):\n            yield f\"data: {event['data']}\\n\\n\".encode(\"utf-8\")\n        yield b\"event: end\\ndata: [DONE]\\n\\n\"\n    return StreamingResponse(gen(), headers=sse_headers())\n```\n\nCross-reference: this is the same anti-buffering pattern used by\n`langchain-langgraph-streaming` (L29) — that skill covers the `astream_events`\nevent shapes and client reconnection; this skill covers the proxy surface.\nIf you see the stream work locally but hang in prod, the header is missing on\nthe upstream response, not the client.\n\n### Step 6 — Secret Manager over `.env` with `pydantic.SecretStr`\n\n`python-dotenv` populates `os.environ`. Anyone with container access runs\n`docker exec \u003Cpod> env` and reads every API key in plain text (P37). Mount\nsecrets from Secret Manager on Cloud Run, Vercel Secrets on Vercel; wrap in\n`pydantic.SecretStr` so `repr()`, logs, and tracebacks print `**********`\ninstead of the value.\n\n```python\nfrom pydantic import SecretStr\nfrom pydantic_settings import BaseSettings, SettingsConfigDict\n\nclass Settings(BaseSettings):\n    model_config = SettingsConfigDict(env_file=None, extra=\"ignore\")\n    anthropic_api_key: SecretStr\n    openai_api_key: SecretStr\n\nsettings = Settings()  # reads real env vars only; never .env in prod\n```\n\nCloud Run: `--set-secrets=VAR=secret-name:latest` (Step 2). Vercel:\n`vercel env add ANTHROPIC_API_KEY production`. Never commit `.env`; add\n`.env*` to `.dockerignore` so it does not enter the build context.\n\nCross-reference: this skill sets the deployment boundary. `langchain-security-basics`\n(P18) covers key-rotation cadence, PII log redaction, and the broader threat\nmodel — get the boundary right here, then layer on P18.\n\n## Output\n\n- Multi-stage Dockerfile, runtime image under 400MB, non-root user, `uvicorn` entrypoint\n- Cloud Run deployment with `--min-instances=1 --cpu-boost --no-cpu-throttling --timeout=3600 --concurrency=80`\n- `vercel.json` with `maxDuration: 60` plus streaming response for requests that may exceed the cap\n- LangServe `add_routes(app, chain, path=\"\u002Fchat\")` behind a FastAPI `lifespan` that closes resource pools on revision retirement\n- SSE responses with `X-Accel-Buffering: no` and `Cache-Control: no-cache` so proxies do not buffer the final `end` event\n- Runtime secrets sourced from Secret Manager \u002F Vercel Secrets and wrapped in `pydantic.SecretStr`; no `.env` in the image\n\n## Platform Decision Table\n\n| Platform | Max timeout | Cold start (p99) | SSE default | Cost baseline (1 agent, 1M req\u002Fmo) | Pick when |\n|----------|-------------|------------------|-------------|------------------------------------|-----------|\n| **Cloud Run** | 3600s | 5-15s (mitigable with min-instances) | Buffers without header override | ~$40-80\u002Fmo + min-instance | Long agents, heavy imports, VPC egress, GCP-native |\n| **Vercel Python** | 10s Hobby \u002F 60s Pro \u002F 900s Enterprise | 2-5s (warmed) | No buffering when streaming response | ~$20\u002Fmo Pro plan flat | Fast iteration, Next.js frontend colocation, short agents |\n| **Fly.io** | No hard cap (per-request soft) | 1-3s (always-on VM) | No default buffer | ~$30\u002Fmo for shared-cpu-1x + 1GB | Need persistent state, low cold start, non-serverless model |\n| **Railway** | 30 min default | 2-4s | No default buffer | ~$25\u002Fmo on Hobby replica | Prototype to production on one platform, Postgres bundled |\n| **Self-hosted (k8s + Nginx)** | Ingress-controlled | 0s (warm pod) | Buffers — requires `proxy_buffering off` | Variable, fixed infra cost | Compliance constraints, existing k8s, egress control |\n\nCloud Run is the default for most LangChain production deployments — the 3600s\ntimeout is the only mainstream serverless option long enough for multi-tool\nagents, and Secret Manager integration is native.\n\n## Error Handling\n\n| Error | Cause | Fix |\n|-------|-------|-----|\n| `FUNCTION_INVOCATION_TIMEOUT` (Vercel) | 10s Python default on Hobby, 60s on Pro (P35) | Set `maxDuration: 60` in `vercel.json`; stream the response |\n| `504 Gateway Timeout` from Cloud Run | Missing `--timeout=3600`; default is 300s | Redeploy with `--timeout=3600 --concurrency=80` |\n| p99 latency 10x p95 | Cold start from Python + LangChain imports (P36) | `--min-instances=1 --cpu-boost`, defer `tiktoken` import to request time |\n| Client hangs forever on SSE stream | Proxy buffering the final `end` event (P46) | Add `X-Accel-Buffering: no` and `Cache-Control: no-cache` headers |\n| `docker exec \u003Cpod> env` shows API keys | `python-dotenv` leaked `.env` into `os.environ` (P37) | Delete `.env` from image, use Secret Manager + `pydantic.SecretStr` |\n| `RuntimeError: Event loop is closed` on shutdown | `asyncpg` \u002F `httpx` pool not closed before revision retirement | Move pool lifecycle into FastAPI `lifespan` context manager |\n| Stream works locally, stalls on Cloud Run | CPU throttling between keepalive pings | Deploy with `--no-cpu-throttling` (CPU-always-allocated) |\n| `ModuleNotFoundError` in Vercel build | `requirements.txt` not generated from `pyproject.toml` | Add build step: `uv export --format requirements-txt > requirements.txt` |\n\n## Examples\n\n### A: Cloud Run with min-instances, secret mounts, VPC egress\n\nOne always-warm replica, two secrets from Secret Manager, egress routed\nthrough a VPC connector for private Postgres. See [Cloud Run Deploy](references\u002Fcloud-run-deploy.md)\nfor the full flag set, revision traffic splitting, and the cost model.\n\n### B: Vercel with streaming and maxDuration\n\n`vercel.json` with `maxDuration: 60` plus the FastAPI streaming entrypoint\nfrom Step 3. The combination gives a 40-second completion a 60-second wall\nand \u003C1s time-to-first-byte. See [Vercel Python Deploy](references\u002Fvercel-python-deploy.md)\nfor Edge Runtime limits and the Vercel Secrets vs env var split.\n\n### C: LangServe behind FastAPI with lifespan\n\nSingle `add_routes` call mounting `\u002Fchat` and `\u002Fchat\u002Fstream` with a shared\nconnection pool closed via `lifespan`. Playground disabled in prod via the\n`__debug__` check. See [LangServe Patterns](references\u002Flangserve-patterns.md)\nfor typed schemas, auth middleware, and raw-handler coexistence.\n\n## Resources\n\n- [Cloud Run: Configuring services](https:\u002F\u002Fcloud.google.com\u002Frun\u002Fdocs\u002Fconfiguring\u002Fservices)\n- [Cloud Run: Always-on CPU (no CPU throttling)](https:\u002F\u002Fcloud.google.com\u002Frun\u002Fdocs\u002Fconfiguring\u002Fcpu-allocation)\n- [Vercel: Python runtime](https:\u002F\u002Fvercel.com\u002Fdocs\u002Ffunctions\u002Fruntimes\u002Fpython)\n- [Vercel: `maxDuration` and streaming](https:\u002F\u002Fvercel.com\u002Fdocs\u002Ffunctions\u002Fconfiguring-functions\u002Fduration)\n- [LangServe: Getting started](https:\u002F\u002Fpython.langchain.com\u002Fdocs\u002Flangserve\u002F)\n- [FastAPI: Lifespan events](https:\u002F\u002Ffastapi.tiangolo.com\u002Fadvanced\u002Fevents\u002F)\n- [Pydantic: `SecretStr`](https:\u002F\u002Fdocs.pydantic.dev\u002Flatest\u002Fapi\u002Ftypes\u002F#pydantic.types.SecretStr)\n- Pack pain catalog: `docs\u002Fpain-catalog.md` (entries P35, P36, P37, P46)\n- Related skills: `langchain-langgraph-streaming` (L29), `langchain-security-basics` (P18)\n",{"name":5,"tags":24,"author":34,"license":15,"version":35,"description":36,"allowed-tools":37,"compatible-with":38},[25,26,27,28,29,30,31,32,33],"saas","langchain","langgraph","python","langchain-1.0","deployment","cloud-run","vercel","langserve","Jeremy Longshore \u003Cjeremy@intentsolutions.io>","2.0.0","Deploy a LangChain 1.0 \u002F LangGraph 1.0 app to Cloud Run, Vercel, or LangServe\ncorrectly — timeouts sized for chain length, cold-start mitigation, SSE\nanti-buffering headers, Secret Manager over `.env`. Use when prepping first\nprod deploy, debugging a stream that hangs behind a proxy, or diagnosing p99\nlatency spikes.\nTrigger with \"langchain deploy\", \"langchain cloud run\", \"langchain vercel python\",\n\"langchain langserve\", \"langchain docker\".\n","Read, Write, Edit, Bash(docker:*), Bash(gcloud:*), Bash(vercel:*)","claude-code, codex","main","99825ad3f3bcc6597ddcc7294a14e6e922f2b67b","skills\u002Fsaas-packs--langchain-py-pack--langchain-deploy-integration",0,"https:\u002F\u002Fgithub.com\u002Fjukeyman\u002Fjukeyman-skills","https:\u002F\u002Fgithub.com\u002Fjukeyman","https:\u002F\u002FRJBUSINESSSOLUTIONS.ORG","Credit and Finance Expert | Developer\r\n\r\nI'm Rick Jefferson, a seasoned credit and finance expert with over a decade of experience in financial literacy,","2026-07-13 07:03:29",{"source":8,"skillName":5,"commands":49,"prompt":59,"zipAvailable":60},[50,53,56],{"tool":51,"command":52},"npx","npx skills add jukeyman\u002Fjukeyman-skills --skill langchain-deploy-integration",{"tool":54,"command":55},"bunx","bunx skills add jukeyman\u002Fjukeyman-skills --skill langchain-deploy-integration",{"tool":57,"command":58},"pnpm","pnpm dlx skills add jukeyman\u002Fjukeyman-skills --skill langchain-deploy-integration","安装技能，技能地址：https:\u002F\u002Fgithub.com\u002Fjukeyman\u002Fjukeyman-skills\u002Fblob\u002Fmain\u002Fskills\u002Fsaas-packs--langchain-py-pack--langchain-deploy-integration\u002FSKILL.md",true,[],[63,70,77,84,91,99],{"skillNo":64,"name":65,"nameZh":6,"description":66,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":67,"categories":68,"keywords":69,"publishedAt":19},"01M79ANS2SM329NEP5ZCX106RR","zustand-store-creator","Create zustand store creator operations. Auto-activating skill for Frontend Development.\nTriggers on: zustand store creator, zustand store creator\nPart of the Frontend Development skill category. Use when working with zustand store creator functionality. Trigger with phrases like \"zustand store creator\", \"zustand creator\", \"zustand\".","https:\u002F\u002Fgithub.com\u002Fjukeyman\u002Fjukeyman-skills\u002Fblob\u002Fmain\u002Fskills\u002Fzustand-store-creator\u002FSKILL.md",[],[],{"skillNo":71,"name":72,"nameZh":6,"description":73,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":74,"categories":75,"keywords":76,"publishedAt":19},"01M79AMZKP4YDEZ7EHG1Y7PP74","zero-trust-config-helper","Configure with zero trust config helper operations. Auto-activating skill for Security Advanced.\nTriggers on: zero trust config helper, zero trust config helper\nPart of the Security Advanced skill category. Use when configuring systems or services. Trigger with phrases like \"zero trust config helper\", \"zero helper\", \"zero\".","https:\u002F\u002Fgithub.com\u002Fjukeyman\u002Fjukeyman-skills\u002Fblob\u002Fmain\u002Fskills\u002Fzero-trust-config-helper\u002FSKILL.md",[],[],{"skillNo":78,"name":79,"nameZh":6,"description":80,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":81,"categories":82,"keywords":83,"publishedAt":19},"01M79AMJSBCEJQNBTYNAHYN5HM","zapier-integration-helper","Assist with zapier integration helper operations. Auto-activating skill for Business Automation.\nTriggers on: zapier integration helper, zapier integration helper\nPart of the Business Automation skill category. Use when working with APIs or building integrations. Trigger with phrases like \"zapier integration helper\", \"zapier helper\", \"zapier\".","https:\u002F\u002Fgithub.com\u002Fjukeyman\u002Fjukeyman-skills\u002Fblob\u002Fmain\u002Fskills\u002Fzapier-integration-helper\u002FSKILL.md",[],[],{"skillNo":85,"name":86,"nameZh":6,"description":87,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":88,"categories":89,"keywords":90,"publishedAt":19},"01M79AHJZGVMARS13VFQYRR1MP","yaml-config-validator","Validate yaml config validator operations. Auto-activating skill for DevOps Basics.\nTriggers on: yaml config validator, yaml config validator\nPart of the DevOps Basics skill category. Use when configuring systems or services. Trigger with phrases like \"yaml config validator\", \"yaml validator\", \"yaml\".","https:\u002F\u002Fgithub.com\u002Fjukeyman\u002Fjukeyman-skills\u002Fblob\u002Fmain\u002Fskills\u002Fyaml-config-validator\u002FSKILL.md",[],[],{"skillNo":92,"name":93,"nameZh":6,"description":94,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":95,"sourceHtmlUrl":96,"categories":97,"keywords":98,"publishedAt":19},"01M79AHGDZRN0020RDQSQDPAM8","xvary-stock-research","Thesis-driven equity analysis from public SEC EDGAR and market data; \u002Fanalyze, \u002Fscore, \u002Fcompare workflows with bundled Python tools (Claude Code, Cursor, Codex).","https:\u002F\u002Fgithub.com\u002Fjukeyman\u002Fjukeyman-skills\u002Fblob\u002Fmain\u002Fskills\u002Fxvary-stock-research\u002FLICENSE","https:\u002F\u002Fgithub.com\u002Fjukeyman\u002Fjukeyman-skills\u002Fblob\u002Fmain\u002Fskills\u002Fxvary-stock-research\u002FSKILL.md",[],[],{"skillNo":100,"name":101,"nameZh":6,"description":102,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":103,"categories":104,"keywords":105,"publishedAt":19},"01M79AHCYJ9FV9RMHQ24HDRN33","xss-vulnerability-scanner","Scan xss vulnerability scanner operations. Auto-activating skill for Security Fundamentals.\nTriggers on: xss vulnerability scanner, xss vulnerability scanner\nPart of the Security Fundamentals skill category. Use when working with xss vulnerability scanner functionality. Trigger with phrases like \"xss vulnerability scanner\", \"xss scanner\", \"xss\".","https:\u002F\u002Fgithub.com\u002Fjukeyman\u002Fjukeyman-skills\u002Fblob\u002Fmain\u002Fskills\u002Fxss-vulnerability-scanner\u002FSKILL.md",[],[]]