Building a Typescript deep research agent

LangChain · Intermediate ·✍️ Prompt Engineering ·8mo ago

Key Takeaways

Building a Typescript deep research agent using LangChain's DeepAgents JS library, involving detailed prompting, search tools, and specialized sub-agents.

Full Transcript

Check this out. I just asked an agent to answer one of the world's greatest debates. Is Messi or Ronaldo the greatest soccer player of all time? This isn't an easy question to answer, and it definitely requires a good amount of research. The agent automatically spawned two parallel sub agents to look into each of their achievements. This meant searching the web over a dozen times, compiling a comprehensive report with cited sources. To be extra thorough, the agent then critiqued its own report and plugged any gaps that it found. In the next few minutes, I'm going to show you how quick and easy it was to build this with deep agents. For those of you who might be hearing the term deep agent for the first time, I think of deep agents as a more powerful tool calling loop. The industry has termed this type of flexible generic agent as an agent harness. Deep agents come with a few important capabilities that extend it beyond the naive tool calling loop. Deep agents come with a planning tool to help keep track of a to-do list for more complex tasks. Deb aents also come with a set of file system tools as well as the ability to connect to a custom file system of your choice. A popular use case is to use the file system to offload context from large tool call results. Another interesting pattern is to use the file system to store the agents actual instructions or memories. So you can improve the deep agent over time. Deep agents also have the ability to spawn sub agents. This makes deep agents super flexible. It can spawn one or more sub aents to work on siloed parts of a complex problem. This keeps the main agents context window clean while sub aents are able to focus on narrower parts of a task. And like I mentioned before, the deep agents agent harness is entirely built on lang chain and langraph. With these core features in mind, let's build a simple deep agent together. The first thing that I'm going to do is install the deep agents package. I've written the code for a sample researcher built on deep agents. As you can see at the top of the file, we've imported create deep agent from the deep agents package. We've also imported the sub aent type, which we'll use to define a couple of sub aents later. For our research deep agent, we're going to start by defining a web search tool with Tavi. This is just a normal lang chain tool. The LLM is going to generate a simple query and we're going to search in Tavi and return five results. For a more custom research agent or for any other agent, you can define more tools that connect to other data sources that are more specific to your use case. Now that we have a tool defined, let's define a few sub aents. Like I mentioned earlier, sub agents are great for working on isolated multi-step tasks. Let's think for a second about how this could be helpful in the context of research. Some research questions can be answered with a single internet search, like what the weather's going to be like tomorrow. Other questions can be a lot more detailed. Let's say I want the agent to plan me a twoe trip to Japan and Korea where I spend a week in each. That's going to take a lot more calls to our web search tool. This is an example where sub aents can really shine. One sub agent can be in charge of planning the Japan leg and another can focus on Korea. These are fairly disperate and isolated tasks and so we can run them in parallel by kicking off multiple research sub aents at once. We define the spec for a research sub aent here. We'll give it a unique name, a description so that the main agent knows when to call it, a system prompt with instructions, and then our internet search tool from above. So that was one sub aent mostly geared towards context engineering and isolation. Sub aents can also be really useful for working on specific tasks that the main agent doesn't want to worry about. An example for our researcher here is a critique sub agent. This sub aent's sole focus is to critique the final report that the main agent wrote. Instead of putting all of these critique instructions into the main agent system prompt, we're isolating these instructions. The main agent is instructed to delegate work to this critique sub agent for this task. Note that our critique agent here is instructed to critique a file from our file system. So, the agent is actually going to write its final report to the file system and use it as a scratch pad for the critique agent to then analyze. Sub aents have access to the same files that are written to by the main agent. Now that we have our tool and our sub aents defined, let's define the main agent. This is the system prompt for the main agent. It has instructions on when to call the research and critique sub aents and it also has instructions on how to write the final report. We tie everything together and create DP agent. Here we specify that we want to use sonnet 4 as our model. The main agent is given the internet search tool to handle simple queries and it's also given the research sub aent for more complex research tasks that can be isolated. We also have the critique sub aent to critique and improve our final report. Now in order for this agent to run, we're going to need to specify a few API keys. I've specified a tably API key for search and anthropic API key for our model. I've also set up Blinksmith tracing so we can take a look at what the agent is doing under the hood. Let's go ahead and run our agent and ask it one of the most hotly contested questions of all time. Who is the goat Messi or Ronaldo? I'm going to let this agent work for a bit and we'll check back in on it when it's done. Awesome. Let's step through our trace. We can see that this ran for good while. One of the first things that the agent did was write down its to-dos. The to-dos include instructions on researching Ronaldo and Messi. Right after this, we can see the agent kicks off two parallel tasks. These are sub aents dedicated to researching Messi and Ronaldo. And we can see that these are kicked off in parallel, which saves us some time. We can see that the web search results themselves within these tasks are a little bit messy, but the sub aents return clean, crisp responses with only the essential information to the main agent. With all of that information, the main agent then writes a final report to its file system with its comprehensive findings. And after that, we can see that we kick off the critique sub aent, which reviews a final report.md. This was just a simple example of a research agent. Deep agents is built to be super flexible as a harness, so you can extend these pre-built tools however you'd like. Hopefully, this gave you a sense of how easy it is to get started building with deep agents. There's a lot more coming down the pipeline in both Python and JavaScript, so stay tuned. Thanks for watching.

Original Description

In this video, we will walk through how to easily build a Typescript deep research agent This builds upon our new DeepAgents JS library. All it involves is some detailed prompting, some search tools, and some specialized sub agents Check out the code here if you'd like to follow along: https://github.com/nhuang-lc/research-deep-agent-js/tree/main
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from LangChain · LangChain · 0 of 60

← Previous Next →
1 Chat With Your Documents Using LangChain + JavaScript
Chat With Your Documents Using LangChain + JavaScript
LangChain
2 LangChain SQL Webinar
LangChain SQL Webinar
LangChain
3 LangChain "OpenAI functions" Webinar
LangChain "OpenAI functions" Webinar
LangChain
4 LangSmith Launch
LangSmith Launch
LangChain
5 LangChain x Pinecone: Supercharging Llama-2 with RAG
LangChain x Pinecone: Supercharging Llama-2 with RAG
LangChain
6 LangChain Expression Language
LangChain Expression Language
LangChain
7 Building LLM applications with LangChain with Lance
Building LLM applications with LangChain with Lance
LangChain
8 Benchmarking Question/Answering Over CSV Data
Benchmarking Question/Answering Over CSV Data
LangChain
9 LangChain "RAG Evaluation" Webinar
LangChain "RAG Evaluation" Webinar
LangChain
10 Fine-tuning in Your Voice Webinar
Fine-tuning in Your Voice Webinar
LangChain
11 Tabular Data Retrieval
Tabular Data Retrieval
LangChain
12 Building an LLM Application with Audio by AssemblyAI
Building an LLM Application with Audio by AssemblyAI
LangChain
13 Superagent Deepdive Webinar
Superagent Deepdive Webinar
LangChain
14 Lessons from Deploying LLMs with LangSmith
Lessons from Deploying LLMs with LangSmith
LangChain
15 Shortwave Assistant Deepdive Webinar
Shortwave Assistant Deepdive Webinar
LangChain
16 Cognitive Architectures for Language Agents
Cognitive Architectures for Language Agents
LangChain
17 Effectively Building with LLMs in the Browser with Jacob
Effectively Building with LLMs in the Browser with Jacob
LangChain
18 Data Privacy for LLMs
Data Privacy for LLMs
LangChain
19 "Theory of Mind" Webinar with Plastic Labs
"Theory of Mind" Webinar with Plastic Labs
LangChain
20 LangChain Templates
LangChain Templates
LangChain
21 Using Natural Language to Query Postgres with Jacob
Using Natural Language to Query Postgres with Jacob
LangChain
22 Building a Research Assistant from Scratch
Building a Research Assistant from Scratch
LangChain
23 Benchmarking RAG over LangChain Docs
Benchmarking RAG over LangChain Docs
LangChain
24 Skeleton-of-Thought: Building a New Template from Scratch
Skeleton-of-Thought: Building a New Template from Scratch
LangChain
25 Benchmarking Methods for Semi-Structured RAG
Benchmarking Methods for Semi-Structured RAG
LangChain
26 LangSmith Highlights: Getting Started
LangSmith Highlights: Getting Started
LangChain
27 LangSmith Highlights: Debugging
LangSmith Highlights: Debugging
LangChain
28 LangSmith Highlights: Datasets
LangSmith Highlights: Datasets
LangChain
29 LangSmith Highlights: Evaluation
LangSmith Highlights: Evaluation
LangChain
30 LangSmith Highlights: Human Annotation
LangSmith Highlights: Human Annotation
LangChain
31 LangSmith Highlights: Monitoring
LangSmith Highlights: Monitoring
LangChain
32 LangSmith Highlights: Hub
LangSmith Highlights: Hub
LangChain
33 SQL Research Assistant
SQL Research Assistant
LangChain
34 Getting Started with Multi-Modal LLMs
Getting Started with Multi-Modal LLMs
LangChain
35 Build a Full Stack RAG App With TypeScript
Build a Full Stack RAG App With TypeScript
LangChain
36 Auto-Prompt Builder (with Hosted LangServe)
Auto-Prompt Builder (with Hosted LangServe)
LangChain
37 LangChain v0.1.0 Launch: Introduction
LangChain v0.1.0 Launch: Introduction
LangChain
38 LangChain v0.1.0 Launch: Observability
LangChain v0.1.0 Launch: Observability
LangChain
39 LangChain v0.1.0 Launch: Integrations
LangChain v0.1.0 Launch: Integrations
LangChain
40 LangChain v0.1.0 Launch: Composability
LangChain v0.1.0 Launch: Composability
LangChain
41 LangChain v0.1.0 Launch: Streaming
LangChain v0.1.0 Launch: Streaming
LangChain
42 LangChain v0.1.0 Launch: Output Parsing
LangChain v0.1.0 Launch: Output Parsing
LangChain
43 LangChain v0.1.0 Launch: Retrieval
LangChain v0.1.0 Launch: Retrieval
LangChain
44 LangChain v0.1.0 Launch: Agents
LangChain v0.1.0 Launch: Agents
LangChain
45 Build and Deploy a RAG app with Pinecone Serverless
Build and Deploy a RAG app with Pinecone Serverless
LangChain
46 Hosted LangServe + LangChain Templates
Hosted LangServe + LangChain Templates
LangChain
47 LangGraph: Intro
LangGraph: Intro
LangChain
48 LangGraph: Agent Executor
LangGraph: Agent Executor
LangChain
49 LangGraph: Chat Agent Executor
LangGraph: Chat Agent Executor
LangChain
50 LangGraph: Human-in-the-Loop
LangGraph: Human-in-the-Loop
LangChain
51 LangGraph: Dynamically Returning a Tool Output Directly
LangGraph: Dynamically Returning a Tool Output Directly
LangChain
52 LangGraph: Respond in a Specific Format
LangGraph: Respond in a Specific Format
LangChain
53 LangGraph: Managing Agent Steps
LangGraph: Managing Agent Steps
LangChain
54 LangGraph: Force-Calling a Tool
LangGraph: Force-Calling a Tool
LangChain
55 LangGraph: Multi-Agent Workflows
LangGraph: Multi-Agent Workflows
LangChain
56 Streaming Events: Introducing a new `stream_events` method
Streaming Events: Introducing a new `stream_events` method
LangChain
57 Building a web RAG chatbot: using LangChain, Exa (prev. Metaphor), LangSmith, and Hosted Langserve
Building a web RAG chatbot: using LangChain, Exa (prev. Metaphor), LangSmith, and Hosted Langserve
LangChain
58 OpenGPTs
OpenGPTs
LangChain
59 Open Source RAG with Nomic's New Embedding Model (and ChromaDB and Ollama)
Open Source RAG with Nomic's New Embedding Model (and ChromaDB and Ollama)
LangChain
60 LangGraph: Persistence
LangGraph: Persistence
LangChain

This video teaches how to build a Typescript deep research agent using LangChain's DeepAgents JS library, covering topics such as agent harness, planning tool, file system tools, and sub-agents.

Key Takeaways
  1. Install the DeepAgents package
  2. Define a web search tool with Tavi
  3. Define sub-agents for research and critique tasks
  4. Define the main agent system prompt
  5. Create a DeepAgent instance with the specified model and tools
  6. Run the agent and analyze its output
💡 Deep agents can be used to build flexible and powerful research agents that can handle complex tasks by spawning sub-agents and utilizing file system tools.

Related Reads

Up next
Loop Engineering Claude Just Changed AI Prompting Forever 🤯
Vskills Certification
Watch →