How to use the LangChain Code Generator
- 1. Pick a patternChoose chat, a prompt chain, structured output, RAG or a tool-calling agent.
- 2. Pick a providerOpenAI, Anthropic, Google or a local Ollama model, plus temperature and options.
- 3. Copy and runInstall with the pip line shown, set your API key and run the code.
What you get
Each pattern is written in current LangChain style with provider packages (langchain-openai, langchain-anthropic, langchain-google-genai, langchain-ollama):
- Chat: a system and user message, a normal call and streaming.
- Chain:
prompt | llm | StrOutputParser()plusbatch. - Structured output:
with_structured_outputfilling a Pydantic model. - RAG: load a text file or PDF, split, embed, index in memory, FAISS or Chroma, then answer from retrieved context.
- Agent:
create_agentwith two@toolfunctions.
Worked example
The default is RAG with OpenAI: a 42-line script using model gpt-6.1-terra and text-embedding-3-small. It loads notes.txt, splits it into 1,000-character chunks with 150 overlap, indexes them in an in-memory vector store, retrieves 4 chunks and answers "using only the context". The install line is pip install -U langchain langchain-openai langchain-text-splitters langchain-community. Switch to an Anthropic agent and you get 29 lines using claude-sonnet-5-5.
How we checked it
We generated all 40 combinations of pattern, provider, vector store and file type. Each one was parsed as Python, every import was run against the real packages (LangChain 1.4) and each model object was created. We couldn't call the providers' APIs from our test machine, so run the code with your own key, and change the model names if newer ones are out. If an import fails on your machine, upgrade the packages with the pip line shown; LangChain moves quickly between versions.
Frequently asked questions
Why Hugging Face embeddings with Anthropic?
Anthropic doesn't offer an embeddings API, so the RAG code uses a small open embedding model that runs on your machine.
Where do I put my API key?
In an environment variable (OPENAI_API_KEY, ANTHROPIC_API_KEY or GOOGLE_API_KEY) or a .env file, never inside code you share.
Is the LangChain code generator free?
Yes. You can open the LangChain code generator and fill in everything without signing up. When you generate code for the first time, we ask once for your name and WhatsApp number; after that it, and every other Uncodemy tool, opens straight away.