After this lesson, you will be able to: Build agents, chains, and memory-backed conversations with LangChain, connect tools, define custom agents, and manage state.
LangChain is the dominant agent framework, opinionated, batteries-included, well-documented. This lesson covers chains, prompt templates, tools, memory, and the modern AgentExecutor pattern. By the end you'll have a working LangChain agent with custom tools.
1. `pip install langchain langchain-anthropic langchain-community`
2. Set ANTHROPIC_API_KEY env var.
3. Test: `python -c "from langchain_anthropic import ChatAnthropic; print(ChatAnthropic(model='claude-sonnet-4-6').invoke('hi').content)"`
Prompt template → LLM → output parser. The 'Hello World' of LangChain.
from langchain_anthropic import ChatAnthropicfrom langchain_core.prompts import ChatPromptTemplatefrom langchain_core.output_parsers import StrOutputParsermodel = ChatAnthropic(model='claude-sonnet-4-6')prompt = ChatPromptTemplate.from_messages([('system', 'You are a helpful coding tutor for beginners.'),('user', 'Explain {concept} in 3 sentences.'),])chain = prompt | model | StrOutputParser()print(chain.invoke({'concept': 'recursion'}))
Built-in tool + custom tool. AgentExecutor handles the loop.
from langchain_anthropic import ChatAnthropicfrom langchain.agents import create_tool_calling_agent, AgentExecutorfrom langchain_core.prompts import ChatPromptTemplatefrom langchain_core.tools import tool@tooldef get_weather(city: str) -> str:"""Returns the weather for a given city."""return f'72F sunny in {city}' # stub@tooldef add_numbers(a: int, b: int) -> int:"""Adds two numbers."""return a + btools = [get_weather, add_numbers]model = ChatAnthropic(model='claude-sonnet-4-6')prompt = ChatPromptTemplate.from_messages([('system', 'You are a helpful assistant.'),('user', '{input}'),('placeholder', '{agent_scratchpad}'),])agent = create_tool_calling_agent(model, tools, prompt)executor = AgentExecutor(agent=agent, tools=tools, verbose=True)print(executor.invoke({'input': 'Whats the weather in Tokyo and what is 17 + 234?'}))
For multi-turn: use `RunnableWithMessageHistory` to wrap any chain with chat-history awareness. Backing store can be in-memory, Redis, Postgres, DynamoDB. The pattern is the same, wrap, configure session ID per request.
Sign in and purchase access to unlock this lesson.