Artificial Intelligence 11 min read

Step-by-Step Guide to Building Multi‑Agent Applications with LangChain LangGraph in Google Colab

This tutorial walks through installing LangChain, LangGraph and related packages in Google Colab, configuring environment variables, defining search and Twitter‑writer tools, constructing a StateGraph workflow with supervisor logic, and executing a multi‑agent LLM pipeline using LangChain’s new multi‑agent capabilities.

Rare Earth Juejin Tech Community
Rare Earth Juejin Tech Community
Rare Earth Juejin Tech Community
Step-by-Step Guide to Building Multi‑Agent Applications with LangChain LangGraph in Google Colab

The article begins with a brief motivation for combining front‑end full‑stack skills with AI in the 2024 job market, then introduces the LangChain‑LangGraph example that integrates a search engine and a Twitter‑writer agent.

It shows how to install the required Python packages in a Colab notebook using the command !pip install -q -U langchain langchain_openai langgraph google-search-results , and explains the meaning of the leading ! and the -q -U flags.

Next, it demonstrates setting up environment variables in Colab with import os from google.colab import userdata os.environ['SERPAPI_API_KEY'] = userdata.get('GOOGLE_API_KEY') os.environ['OPENAI_API_KEY'] = userdata.get('OPENAI_API_KEY') os.environ['LANGCHAIN_TRACING_V2'] = "true" os.environ['LANGCHAIN_PROJECT'] = "LangGraph" os.environ['LANGCHAIN_API_KEY'] = userdata.get('LANGSMITH_API_KEY') , explaining that userdata stores API keys for later retrieval.

The guide mentions LangSmith as a platform for production‑grade LLM applications, noting that its usage will be covered in a future article.

A quick test of the SerpAPI wrapper is provided: from langchain_community.utilities import SerpAPIWrapper search = SerpAPIWrapper() search.run("Obama's first name?") , illustrating how the langchain_community package extends LangChain with third‑party tools.

All necessary imports are listed, including standard libraries ( functools, operator, requests, os, json ) and LangChain modules ( AgentExecutor, create_openai_tools_agent, BaseMessage, HumanMessage, JsonOutputFunctionsParser, ChatPromptTemplate, MessagesPlaceholder, StateGraph, END, tool, ChatOpenAI ), as well as typing utilities.

The LLM is instantiated with llm = ChatOpenAI(model="gpt-4-turbo-preview") , noting that gpt-4 performs better on complex agents than gpt-3.5-turbo .

Two tool functions are defined using the @tool decorator: web_search (calls SerpAPIWrapper ) and write_tweet (uses a ChatOpenAI instance with system and human messages to generate a tweet under 140 characters).

A TypedDict AgentState is created to hold a list of messages and a next field, forming the graph state for LangGraph.

The helper create_agent builds an agent from an LLM, a list of tools, and a system prompt, returning an AgentExecutor . An agent_node function runs the agent and returns new messages.

A supervisor chain is constructed with a system prompt that routes between the two workers ( Search_Engine and Twitter_Writer ) or finishes, using a function definition route and JsonOutputFunctionsParser to parse the routing decision.

The workflow is assembled by adding the search and Twitter nodes, the supervisor node, and defining edges: each worker points to the supervisor, and the supervisor has conditional edges based on the next field, with END terminating the graph.

Finally, the compiled graph is streamed with an initial human message ( HumanMessage(content="Write a tweet about LangChain news") ), printing each intermediate output until the graph finishes.

The article concludes that the hands‑on exploration deepens understanding of LangGraph, prompts, and agents, while noting that LangGraph is currently less intuitive than AutoGen’s chat‑based multi‑agent approach.

pythonAILLMLangChainmulti-agentGoogle ColabLangGraph
Rare Earth Juejin Tech Community
Written by

Rare Earth Juejin Tech Community

Juejin, a tech community that helps developers grow.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.