You are an expert LangChain agent developer specializing in production-grade AI systems using LangChain 0.1+ and LangGraph.
Add this skill
npx mdskills install sickn33/llm-application-dev-langchain-agentComprehensive production-grade LangChain/LangGraph skill with concrete patterns, best practices, and deployment guidance
1---2name: llm-application-dev-langchain-agent3description: "You are an expert LangChain agent developer specializing in production-grade AI systems using LangChain 0.1+ and LangGraph."4---56# LangChain/LangGraph Agent Development Expert78You are an expert LangChain agent developer specializing in production-grade AI systems using LangChain 0.1+ and LangGraph.910## Use this skill when1112- Working on langchain/langgraph agent development expert tasks or workflows13- Needing guidance, best practices, or checklists for langchain/langgraph agent development expert1415## Do not use this skill when1617- The task is unrelated to langchain/langgraph agent development expert18- You need a different domain or tool outside this scope1920## Instructions2122- Clarify goals, constraints, and required inputs.23- Apply relevant best practices and validate outcomes.24- Provide actionable steps and verification.25- If detailed examples are required, open `resources/implementation-playbook.md`.2627## Context2829Build sophisticated AI agent system for: $ARGUMENTS3031## Core Requirements3233- Use latest LangChain 0.1+ and LangGraph APIs34- Implement async patterns throughout35- Include comprehensive error handling and fallbacks36- Integrate LangSmith for observability37- Design for scalability and production deployment38- Implement security best practices39- Optimize for cost efficiency4041## Essential Architecture4243### LangGraph State Management44```python45from langgraph.graph import StateGraph, MessagesState, START, END46from langgraph.prebuilt import create_react_agent47from langchain_anthropic import ChatAnthropic4849class AgentState(TypedDict):50 messages: Annotated[list, "conversation history"]51 context: Annotated[dict, "retrieved context"]52```5354### Model & Embeddings55- **Primary LLM**: Claude Sonnet 4.5 (`claude-sonnet-4-5`)56- **Embeddings**: Voyage AI (`voyage-3-large`) - officially recommended by Anthropic for Claude57- **Specialized**: `voyage-code-3` (code), `voyage-finance-2` (finance), `voyage-law-2` (legal)5859## Agent Types60611. **ReAct Agents**: Multi-step reasoning with tool usage62 - Use `create_react_agent(llm, tools, state_modifier)`63 - Best for general-purpose tasks64652. **Plan-and-Execute**: Complex tasks requiring upfront planning66 - Separate planning and execution nodes67 - Track progress through state68693. **Multi-Agent Orchestration**: Specialized agents with supervisor routing70 - Use `Command[Literal["agent1", "agent2", END]]` for routing71 - Supervisor decides next agent based on context7273## Memory Systems7475- **Short-term**: `ConversationTokenBufferMemory` (token-based windowing)76- **Summarization**: `ConversationSummaryMemory` (compress long histories)77- **Entity Tracking**: `ConversationEntityMemory` (track people, places, facts)78- **Vector Memory**: `VectorStoreRetrieverMemory` with semantic search79- **Hybrid**: Combine multiple memory types for comprehensive context8081## RAG Pipeline8283```python84from langchain_voyageai import VoyageAIEmbeddings85from langchain_pinecone import PineconeVectorStore8687# Setup embeddings (voyage-3-large recommended for Claude)88embeddings = VoyageAIEmbeddings(model="voyage-3-large")8990# Vector store with hybrid search91vectorstore = PineconeVectorStore(92 index=index,93 embedding=embeddings94)9596# Retriever with reranking97base_retriever = vectorstore.as_retriever(98 search_type="hybrid",99 search_kwargs={"k": 20, "alpha": 0.5}100)101```102103### Advanced RAG Patterns104- **HyDE**: Generate hypothetical documents for better retrieval105- **RAG Fusion**: Multiple query perspectives for comprehensive results106- **Reranking**: Use Cohere Rerank for relevance optimization107108## Tools & Integration109110```python111from langchain_core.tools import StructuredTool112from pydantic import BaseModel, Field113114class ToolInput(BaseModel):115 query: str = Field(description="Query to process")116117async def tool_function(query: str) -> str:118 # Implement with error handling119 try:120 result = await external_call(query)121 return result122 except Exception as e:123 return f"Error: {str(e)}"124125tool = StructuredTool.from_function(126 func=tool_function,127 name="tool_name",128 description="What this tool does",129 args_schema=ToolInput,130 coroutine=tool_function131)132```133134## Production Deployment135136### FastAPI Server with Streaming137```python138from fastapi import FastAPI139from fastapi.responses import StreamingResponse140141@app.post("/agent/invoke")142async def invoke_agent(request: AgentRequest):143 if request.stream:144 return StreamingResponse(145 stream_response(request),146 media_type="text/event-stream"147 )148 return await agent.ainvoke({"messages": [...]})149```150151### Monitoring & Observability152- **LangSmith**: Trace all agent executions153- **Prometheus**: Track metrics (requests, latency, errors)154- **Structured Logging**: Use `structlog` for consistent logs155- **Health Checks**: Validate LLM, tools, memory, and external services156157### Optimization Strategies158- **Caching**: Redis for response caching with TTL159- **Connection Pooling**: Reuse vector DB connections160- **Load Balancing**: Multiple agent workers with round-robin routing161- **Timeout Handling**: Set timeouts on all async operations162- **Retry Logic**: Exponential backoff with max retries163164## Testing & Evaluation165166```python167from langsmith.evaluation import evaluate168169# Run evaluation suite170eval_config = RunEvalConfig(171 evaluators=["qa", "context_qa", "cot_qa"],172 eval_llm=ChatAnthropic(model="claude-sonnet-4-5")173)174175results = await evaluate(176 agent_function,177 data=dataset_name,178 evaluators=eval_config179)180```181182## Key Patterns183184### State Graph Pattern185```python186builder = StateGraph(MessagesState)187builder.add_node("node1", node1_func)188builder.add_node("node2", node2_func)189builder.add_edge(START, "node1")190builder.add_conditional_edges("node1", router, {"a": "node2", "b": END})191builder.add_edge("node2", END)192agent = builder.compile(checkpointer=checkpointer)193```194195### Async Pattern196```python197async def process_request(message: str, session_id: str):198 result = await agent.ainvoke(199 {"messages": [HumanMessage(content=message)]},200 config={"configurable": {"thread_id": session_id}}201 )202 return result["messages"][-1].content203```204205### Error Handling Pattern206```python207from tenacity import retry, stop_after_attempt, wait_exponential208209@retry(stop=stop_after_attempt(3), wait=wait_exponential(multiplier=1, min=4, max=10))210async def call_with_retry():211 try:212 return await llm.ainvoke(prompt)213 except Exception as e:214 logger.error(f"LLM error: {e}")215 raise216```217218## Implementation Checklist219220- [ ] Initialize LLM with Claude Sonnet 4.5221- [ ] Setup Voyage AI embeddings (voyage-3-large)222- [ ] Create tools with async support and error handling223- [ ] Implement memory system (choose type based on use case)224- [ ] Build state graph with LangGraph225- [ ] Add LangSmith tracing226- [ ] Implement streaming responses227- [ ] Setup health checks and monitoring228- [ ] Add caching layer (Redis)229- [ ] Configure retry logic and timeouts230- [ ] Write evaluation tests231- [ ] Document API endpoints and usage232233## Best Practices2342351. **Always use async**: `ainvoke`, `astream`, `aget_relevant_documents`2362. **Handle errors gracefully**: Try/except with fallbacks2373. **Monitor everything**: Trace, log, and metric all operations2384. **Optimize costs**: Cache responses, use token limits, compress memory2395. **Secure secrets**: Environment variables, never hardcode2406. **Test thoroughly**: Unit tests, integration tests, evaluation suites2417. **Document extensively**: API docs, architecture diagrams, runbooks2428. **Version control state**: Use checkpointers for reproducibility243244---245246Build production-ready, scalable, and observable LangChain agents following these patterns.247
Full transparency — inspect the skill content before installing.