advanced40 minutesLição 1 de 10

Implantação

Implante aplicações LangGraph com LangGraph Cloud/Platform, configure APIs do grafo, gerencie opções de implantação e escale para produção.

Implantação

Deploying LangGraph applications to production requires understanding LangGraph Cloud (Platform), API configuration, authentication, scaling, and operational considerations.


Visão Geral do LangGraph Cloud/Platform

LangGraph Platform provides a managed deployment infrastructure for LangGraph applications:

FuncionalidadeDescrição
Managed APIAutomatic REST API generation from your graph
ScalingAuto-scaling based on demand
PersistenceManaged PostgreSQL for checkpointing
AutenticaçãoAPI key and OAuth support
MonitoringBuilt-in LangSmith integration
StreamingServer-Sent Eventos (SSE) for real-time output

Arquitetura de Implantação

┌─────────────┐ ┌──────────────┐ ┌─────────────┐ │ Client │────▶│ LangGraph │────▶│ LLM │ │ (App/Web) │ │ Platform │ │ APIs │ └─────────────┘ └──────────────┘ └─────────────┘ │ ▼ ┌──────────────┐ ┌─────────────┐ │ PostgreSQL │ │ LangSmith │ │ (State/Pers.)│ │ (Observ.) │ └──────────────┘ └─────────────┘

Configuração da API do Grafo

Create a langgraph.json configuration file at your project root:

json
{ "name": "my-agent", "version": "1.0.0", "graphs": { "agent": "./src/agent.py:graph" }, "dependencies": { "pip": ["langchain-openai", "tavily-python"], "python": "3.11" }, "env": { "OPENAI_API_KEY": "sk-...", "TAVILY_API_KEY": "tvly-..." }, "checkpointer": { "type": "postgres", "url": "postgresql://user:pass@host:5432/langgraph" } }

[!IMPORTANTE] The graphs field maps endpoint names to Python import paths. The value "./src/agent.py:graph" means "import graph from src/agent.py".


Implantação com Dockerfile

For custom deployments, use the official LangGraph Docker image:

dockerfile
FROM langchain/langgraph:latest WORKDIR /app COPY pyproject.toml . RUN pip install -e . COPY . . # The graph is auto-detected from langgraph.json EXPOSE 8080 CMD ["langgraph", "serve"]

Opções de Implantação

Option 1: LangGraph Cloud (Managed)

bash
# Install CLI pip install langgraph-cli # Deploy to LangGraph Cloud langgraph deploy --project my-agent # Set environment variables langgraph secrets set OPENAI_API_KEY sk-...

Option 2: Self-Hosted Docker

bash
# Build the Docker image docker build -t my-agent . # Run with environment variables docker run -p 8080:8080 \ -e OPENAI_API_KEY=sk-... \ -e DATABASE_URL=postgresql://... \ my-agent

Option 3: Kubernetes

yaml
apiVersion: apps/v1 kind: Deployment metadata: name: langgraph-agent spec: replicas: 3 selector: matchLabels: app: langgraph-agent template: metadata: labels: app: langgraph-agent spec: containers: - name: agent image: my-agent:latest ports: - containerPort: 8080 env: - name: OPENAI_API_KEY valueFrom: secretKeyRef: name: api-keys key: openai-key - name: DATABASE_URL valueFrom: secretKeyRef: name: db-credentials key: url

[!NOTA] For production, use Kubernetes secrets or a vault service for API keys. Never hardcode secrets in your code or config files.


Endpoints da API

LangGraph Platform generates these REST endpoints:

EndpointMethodDescrição
/threadsPOSTCreate a new conversation thread
/threads/{id}/runsPOSTInvoke the graph on a thread
/threads/{id}/stateGETGet thread state
/threads/{id}/statePATCHUpdate thread state
/threads/{id}/runs/{run_id}/streamGETStream execution events

Client Example

python
import requests API_URL = "https://my-agent.langgraph.app" # Create a thread resp = requests.post(f"{API_URL}/threads", json={}) thread_id = resp.json()["thread_id"] # Invoke the graph resp = requests.post( f"{API_URL}/threads/{thread_id}/runs", json={"input": {"query": "What is LangGraph?"}} ) print(resp.json()) # Get state state = requests.get(f"{API_URL}/threads/{thread_id}/state") print(state.json())

Streaming da API Implantada

python
# Stream events via SSE import json import requests with requests.post( f"{API_URL}/threads/{thread_id}/runs/stream", json={"input": {"query": "Write a poem"}}, stream=True ) as resp: for line in resp.iter_lines(): if line: event = json.loads(line.decode("utf-8").removeprefix("data: ")) print(event)

Autenticação

python
# API Key authentication headers = { "X-Api-Key": "lgv2_...", "Content-Type": "application/json" } resp = requests.post( f"{API_URL}/threads", headers=headers, json={} )

[!AVISO] Rotate API keys regularly. Use short-lived keys for production and revoke compromised keys immediately.


Considerações de Escala

DimensionEstratégia
Concurrent usersHorizontal scaling (more replicas)
Long-running graphsAsync execution with webhook callbacks
DatabasePostgreSQL connection pooling (PgBouncer)
LLM rate limitsQueue requests, implement retry with backoff
MemoryMonitor checkpoint size, trim old checkpoints
Cold startsKeep minimum replicas warm

Conexão com Banco de Dados Pooling

python
from langgraph.checkpoint.postgres import PostgresSaver # Use connection pooling for production checkpointer = PostgresSaver.from_conn_string( "postgresql://user:pass@host:5432/langgraph", pool_size=10, max_overflow=20 )

Verificações de Saúde

python
from fastapi import FastAPI from langgraph.graph import StateGraph app = FastAPI() graph_app = builder.compile(checkpointer=checkpointer) @app.get("/health") def health(): return {"status": "ok", "graph": "ready"} @app.post("/invoke") def invoke(input: dict): result = graph_app.invoke(input) return result

Perguntas Práticas

Practice Question

What file configures LangGraph Platform deployment?

Practice Question

What does the 'graphs' field in langgraph.json specify?

Practice Question

What database is recommended for production checkpointing?

Practice Question

Which HTTP method creates a new conversation thread?

Practice Question

How do you invoke a graph on an existing thread via the API?

Practice Question

How should secrets be handled in production LangGraph deployments?

Practice Question

What is a recommended strategy for LLM rate limits in production?

Practice Question

How does LangGraph Platform handle concurrent users?

Practice Question

What protocol does LangGraph Platform use for real-time streaming?

Practice Question

What information does a health check endpoint typically return?


[!SUCESSO]

Principais Conclusões

  • langgraph.json configures LangGraph Platform deployment
  • Three deployment options: LangGraph Cloud, self-hosted Docker, Kubernetes
  • REST API: POST /threads → POST /threads/{id}/runs → GET /threads/{id}/state
  • PostgreSQL for production checkpointing; connection pooling for scale
  • API key authentication with regular rotation
  • Horizontal scaling for concurrent users; retry with backoff for rate limits
  • SSE for real-time streaming of execution events and tokens
  • Health checks for monitoring and orchestration
Progresso10%