█
LastWrite
  • > Curriculum
  • > Pricing
  • > For Educators
  • > About
  • > Contact
Log InGet Started

Questions, concerns, bug reports, or suggestions? We read every message, write to us at [email protected].

More ways to reach us →
LastWrite

Structured computer science lessons for aspiring developers and security professionals.

[email protected]

(201) 785-7951

Mon–Fri, 9 AM–5 PM EST

Learn

  • Curriculum
  • Pricing

Company

  • About
  • For Educators & Schools
  • Contact Us

Legal

  • Terms of Service
  • Privacy Policy
© 2026 LastWrite. All rights reserved.
Curriculum/AI Agents/Code-First Agents/LangChain: The Agent Framework
70 minIntermediate

LangChain: The Agent Framework

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.

Prerequisites:Code-First Agent Fundamentals: Python, ReAct, and Tool Calling

Install + setup

  1. 1

    1. `pip install langchain langchain-anthropic langchain-community`

  2. 2

    2. Set ANTHROPIC_API_KEY env var.

  3. 3

    3. Test: `python -c "from langchain_anthropic import ChatAnthropic; print(ChatAnthropic(model='claude-sonnet-4-6').invoke('hi').content)"`

Your first chain

Prompt template → LLM → output parser. The 'Hello World' of LangChain.

python
from langchain_anthropic import ChatAnthropic
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
model = 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'}))

💡 The | operator (LCEL)

LangChain Expression Language. `prompt | model | parser` chains them like Unix pipes. Type-safe, streamable, batchable. This is the modern LangChain idiom, older `LLMChain` is deprecated.

Tool-using agent

Built-in tool + custom tool. AgentExecutor handles the loop.

python
from langchain_anthropic import ChatAnthropic
from langchain.agents import create_tool_calling_agent, AgentExecutor
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.tools import tool
@tool
def get_weather(city: str) -> str:
"""Returns the weather for a given city."""
return f'72F sunny in {city}' # stub
@tool
def add_numbers(a: int, b: int) -> int:
"""Adds two numbers."""
return a + b
tools = [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?'}))

Memory in LangChain

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.

Sign in to purchase
←Code-First Agent Fundamentals: Python, ReAct, and Tool Calling
Back to Code-First Agents
LangGraph: Stateful Multi-Step Agent Graphs→