Design LLM applications using the LangChain framework with agents, memory, and tool integration patterns. Use when building LangChain applications, implementing AI agents, or creating complex LLM workflows.
Add this skill
npx mdskills install sickn33/langchain-architectureComprehensive LangChain guide with clear patterns, examples, and production best practices
1---2name: langchain-architecture3description: Design LLM applications using the LangChain framework with agents, memory, and tool integration patterns. Use when building LangChain applications, implementing AI agents, or creating complex LLM workflows.4---56# LangChain Architecture78Master the LangChain framework for building sophisticated LLM applications with agents, chains, memory, and tool integration.910## Do not use this skill when1112- The task is unrelated to langchain architecture13- You need a different domain or tool outside this scope1415## Instructions1617- Clarify goals, constraints, and required inputs.18- Apply relevant best practices and validate outcomes.19- Provide actionable steps and verification.20- If detailed examples are required, open `resources/implementation-playbook.md`.2122## Use this skill when2324- Building autonomous AI agents with tool access25- Implementing complex multi-step LLM workflows26- Managing conversation memory and state27- Integrating LLMs with external data sources and APIs28- Creating modular, reusable LLM application components29- Implementing document processing pipelines30- Building production-grade LLM applications3132## Core Concepts3334### 1. Agents35Autonomous systems that use LLMs to decide which actions to take.3637**Agent Types:**38- **ReAct**: Reasoning + Acting in interleaved manner39- **OpenAI Functions**: Leverages function calling API40- **Structured Chat**: Handles multi-input tools41- **Conversational**: Optimized for chat interfaces42- **Self-Ask with Search**: Decomposes complex queries4344### 2. Chains45Sequences of calls to LLMs or other utilities.4647**Chain Types:**48- **LLMChain**: Basic prompt + LLM combination49- **SequentialChain**: Multiple chains in sequence50- **RouterChain**: Routes inputs to specialized chains51- **TransformChain**: Data transformations between steps52- **MapReduceChain**: Parallel processing with aggregation5354### 3. Memory55Systems for maintaining context across interactions.5657**Memory Types:**58- **ConversationBufferMemory**: Stores all messages59- **ConversationSummaryMemory**: Summarizes older messages60- **ConversationBufferWindowMemory**: Keeps last N messages61- **EntityMemory**: Tracks information about entities62- **VectorStoreMemory**: Semantic similarity retrieval6364### 4. Document Processing65Loading, transforming, and storing documents for retrieval.6667**Components:**68- **Document Loaders**: Load from various sources69- **Text Splitters**: Chunk documents intelligently70- **Vector Stores**: Store and retrieve embeddings71- **Retrievers**: Fetch relevant documents72- **Indexes**: Organize documents for efficient access7374### 5. Callbacks75Hooks for logging, monitoring, and debugging.7677**Use Cases:**78- Request/response logging79- Token usage tracking80- Latency monitoring81- Error handling82- Custom metrics collection8384## Quick Start8586```python87from langchain.agents import AgentType, initialize_agent, load_tools88from langchain.llms import OpenAI89from langchain.memory import ConversationBufferMemory9091# Initialize LLM92llm = OpenAI(temperature=0)9394# Load tools95tools = load_tools(["serpapi", "llm-math"], llm=llm)9697# Add memory98memory = ConversationBufferMemory(memory_key="chat_history")99100# Create agent101agent = initialize_agent(102 tools,103 llm,104 agent=AgentType.CONVERSATIONAL_REACT_DESCRIPTION,105 memory=memory,106 verbose=True107)108109# Run agent110result = agent.run("What's the weather in SF? Then calculate 25 * 4")111```112113## Architecture Patterns114115### Pattern 1: RAG with LangChain116```python117from langchain.chains import RetrievalQA118from langchain.document_loaders import TextLoader119from langchain.text_splitter import CharacterTextSplitter120from langchain.vectorstores import Chroma121from langchain.embeddings import OpenAIEmbeddings122123# Load and process documents124loader = TextLoader('documents.txt')125documents = loader.load()126127text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=200)128texts = text_splitter.split_documents(documents)129130# Create vector store131embeddings = OpenAIEmbeddings()132vectorstore = Chroma.from_documents(texts, embeddings)133134# Create retrieval chain135qa_chain = RetrievalQA.from_chain_type(136 llm=llm,137 chain_type="stuff",138 retriever=vectorstore.as_retriever(),139 return_source_documents=True140)141142# Query143result = qa_chain({"query": "What is the main topic?"})144```145146### Pattern 2: Custom Agent with Tools147```python148from langchain.agents import Tool, AgentExecutor149from langchain.agents.react.base import ReActDocstoreAgent150from langchain.tools import tool151152@tool153def search_database(query: str) -> str:154 """Search internal database for information."""155 # Your database search logic156 return f"Results for: {query}"157158@tool159def send_email(recipient: str, content: str) -> str:160 """Send an email to specified recipient."""161 # Email sending logic162 return f"Email sent to {recipient}"163164tools = [search_database, send_email]165166agent = initialize_agent(167 tools,168 llm,169 agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,170 verbose=True171)172```173174### Pattern 3: Multi-Step Chain175```python176from langchain.chains import LLMChain, SequentialChain177from langchain.prompts import PromptTemplate178179# Step 1: Extract key information180extract_prompt = PromptTemplate(181 input_variables=["text"],182 template="Extract key entities from: {text}\n\nEntities:"183)184extract_chain = LLMChain(llm=llm, prompt=extract_prompt, output_key="entities")185186# Step 2: Analyze entities187analyze_prompt = PromptTemplate(188 input_variables=["entities"],189 template="Analyze these entities: {entities}\n\nAnalysis:"190)191analyze_chain = LLMChain(llm=llm, prompt=analyze_prompt, output_key="analysis")192193# Step 3: Generate summary194summary_prompt = PromptTemplate(195 input_variables=["entities", "analysis"],196 template="Summarize:\nEntities: {entities}\nAnalysis: {analysis}\n\nSummary:"197)198summary_chain = LLMChain(llm=llm, prompt=summary_prompt, output_key="summary")199200# Combine into sequential chain201overall_chain = SequentialChain(202 chains=[extract_chain, analyze_chain, summary_chain],203 input_variables=["text"],204 output_variables=["entities", "analysis", "summary"],205 verbose=True206)207```208209## Memory Management Best Practices210211### Choosing the Right Memory Type212```python213# For short conversations (< 10 messages)214from langchain.memory import ConversationBufferMemory215memory = ConversationBufferMemory()216217# For long conversations (summarize old messages)218from langchain.memory import ConversationSummaryMemory219memory = ConversationSummaryMemory(llm=llm)220221# For sliding window (last N messages)222from langchain.memory import ConversationBufferWindowMemory223memory = ConversationBufferWindowMemory(k=5)224225# For entity tracking226from langchain.memory import ConversationEntityMemory227memory = ConversationEntityMemory(llm=llm)228229# For semantic retrieval of relevant history230from langchain.memory import VectorStoreRetrieverMemory231memory = VectorStoreRetrieverMemory(retriever=retriever)232```233234## Callback System235236### Custom Callback Handler237```python238from langchain.callbacks.base import BaseCallbackHandler239240class CustomCallbackHandler(BaseCallbackHandler):241 def on_llm_start(self, serialized, prompts, **kwargs):242 print(f"LLM started with prompts: {prompts}")243244 def on_llm_end(self, response, **kwargs):245 print(f"LLM ended with response: {response}")246247 def on_llm_error(self, error, **kwargs):248 print(f"LLM error: {error}")249250 def on_chain_start(self, serialized, inputs, **kwargs):251 print(f"Chain started with inputs: {inputs}")252253 def on_agent_action(self, action, **kwargs):254 print(f"Agent taking action: {action}")255256# Use callback257agent.run("query", callbacks=[CustomCallbackHandler()])258```259260## Testing Strategies261262```python263import pytest264from unittest.mock import Mock265266def test_agent_tool_selection():267 # Mock LLM to return specific tool selection268 mock_llm = Mock()269 mock_llm.predict.return_value = "Action: search_database\nAction Input: test query"270271 agent = initialize_agent(tools, mock_llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION)272273 result = agent.run("test query")274275 # Verify correct tool was selected276 assert "search_database" in str(mock_llm.predict.call_args)277278def test_memory_persistence():279 memory = ConversationBufferMemory()280281 memory.save_context({"input": "Hi"}, {"output": "Hello!"})282283 assert "Hi" in memory.load_memory_variables({})['history']284 assert "Hello!" in memory.load_memory_variables({})['history']285```286287## Performance Optimization288289### 1. Caching290```python291from langchain.cache import InMemoryCache292import langchain293294langchain.llm_cache = InMemoryCache()295```296297### 2. Batch Processing298```python299# Process multiple documents in parallel300from langchain.document_loaders import DirectoryLoader301from concurrent.futures import ThreadPoolExecutor302303loader = DirectoryLoader('./docs')304docs = loader.load()305306def process_doc(doc):307 return text_splitter.split_documents([doc])308309with ThreadPoolExecutor(max_workers=4) as executor:310 split_docs = list(executor.map(process_doc, docs))311```312313### 3. Streaming Responses314```python315from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler316317llm = OpenAI(streaming=True, callbacks=[StreamingStdOutCallbackHandler()])318```319320## Resources321322- **references/agents.md**: Deep dive on agent architectures323- **references/memory.md**: Memory system patterns324- **references/chains.md**: Chain composition strategies325- **references/document-processing.md**: Document loading and indexing326- **references/callbacks.md**: Monitoring and observability327- **assets/agent-template.py**: Production-ready agent template328- **assets/memory-config.yaml**: Memory configuration examples329- **assets/chain-example.py**: Complex chain examples330331## Common Pitfalls3323331. **Memory Overflow**: Not managing conversation history length3342. **Tool Selection Errors**: Poor tool descriptions confuse agents3353. **Context Window Exceeded**: Exceeding LLM token limits3364. **No Error Handling**: Not catching and handling agent failures3375. **Inefficient Retrieval**: Not optimizing vector store queries338339## Production Checklist340341- [ ] Implement proper error handling342- [ ] Add request/response logging343- [ ] Monitor token usage and costs344- [ ] Set timeout limits for agent execution345- [ ] Implement rate limiting346- [ ] Add input validation347- [ ] Test with edge cases348- [ ] Set up observability (callbacks)349- [ ] Implement fallback strategies350- [ ] Version control prompts and configurations351
Full transparency — inspect the skill content before installing.