Set up LangChain for AI workflows
✓Works with OpenClaudeYou are a Python AI/ML engineer. The user wants to set up LangChain for building AI workflows with language models, including API configuration, basic chains, and memory management.
What to check first
- Run
python --versionto confirm Python 3.8+ is installed - Check
pip list | grep langchainto see if LangChain is already installed - Verify you have an API key from OpenAI, Anthropic, or another supported LLM provider
Steps
- Install LangChain and required dependencies with
pip install langchain langchain-openai python-dotenv - Create a
.envfile in your project root and add your API key:OPENAI_API_KEY=sk-... - Load environment variables in your Python script using
python-dotenv:from dotenv import load_dotenvandload_dotenv() - Initialize the language model by importing
ChatOpenAIfromlangchain_openaiand instantiate it withmodel="gpt-3.5-turbo" - Create a simple chain using
LLMChainfromlangchain.chainspaired with aPromptTemplatefor templated inputs - Add conversation memory using
ConversationBufferMemoryfromlangchain.memoryto persist chat history across turns - Test the chain by calling
chain.run()or.invoke()with sample input and verify output is returned - Configure additional parameters like
temperature,max_tokens, andtop_pon the LLM instance for response tuning
Code
import os
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
from langchain.memory import ConversationBufferMemory
# Load environment variables
load_dotenv()
api_key = os.getenv("OPENAI_API_KEY")
# Initialize the language model
llm = ChatOpenAI(
model="gpt-3.5-turbo",
temperature=0.7,
max_tokens=256,
api_key=api_key
)
# Define a prompt template
prompt_template = PromptTemplate(
input_variables=["topic"],
template="Tell me something interesting about {topic} in 2 sentences."
)
# Initialize memory for conversation history
memory = ConversationBufferMemory(
memory_key="chat_history",
return_messages=True
)
# Create a chain
chain = LLMChain(
llm=llm,
prompt=prompt_template,
memory=memory,
verbose=True
)
# Run the chain
response = chain.invoke({"topic": "artificial intelligence"})
print("Response:", response["text"])
# Access conversation history
print("Chat History:", memory.chat_memory.messages)
# Run another query to
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Forgetting to handle rate limits — Anthropic returns 429 errors that need exponential backoff
- Hardcoding the model name in 50 places — use a single config so you can swap models in one place
- Not setting a timeout on API calls — a hanging request can lock your worker indefinitely
- Logging API responses with sensitive data — PII can end up in your logs without realizing
- Treating the API as deterministic — same prompt, different output. Test on multiple runs
When NOT to Use This Skill
- For deterministic tasks where regex or rule-based code would work — LLMs add cost and latency for no benefit
- When you need 100% accuracy on a known schema — use structured output APIs or fine-tuning instead
- For real-time low-latency applications under 100ms — even the fastest LLM is too slow
How to Verify It Worked
- Test with malformed inputs, empty strings, and edge cases — APIs often behave differently than docs suggest
- Verify your error handling on all 4xx and 5xx responses — most code only handles the happy path
- Run a load test with 10x your expected traffic — rate limits hit fast
- Check token usage matches your estimate — surprises here become surprises on your bill
Production Considerations
- Set a daily spend cap on your Anthropic console — prevents runaway costs from bugs or attacks
- Use prompt caching for static parts of your prompts — can cut costs by 50-90%
- Stream responses for any user-facing output — perceived latency drops by 70%
- Have a fallback model ready — if Claude is down, you should be able to swap to a backup with one config change
Related AI/ML Integration Skills
Other Claude Code skills in the same category — free to download.
OpenAI Integration
Integrate OpenAI API with best practices
Claude API Setup
Set up Claude/Anthropic API integration
Embedding Search
Implement vector embedding search
RAG Pipeline
Build Retrieval-Augmented Generation pipeline
Prompt Template
Create reusable prompt templates with variables
AI Streaming
Implement streaming AI responses
Model Comparison
Compare responses from multiple AI models
AI Rate Limiter
Implement rate limiting for AI API calls
Want a AI/ML Integration skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.