Skip to content

Commit 0728e59

Browse files
committed
swap chapters
1 parent cddb6eb commit 0728e59

4 files changed

Lines changed: 590 additions & 500 deletions

File tree

_toc.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ parts:
1717
- caption: "3. LLM Agents"
1818
chapters:
1919
- file: chapter_3/agents
20-
- file: chapter_3/agent_implementation
2120
- file: chapter_3/aviary_agent_implementation
21+
- file: chapter_3/agent_implementation
2222
- file: chapter_3/agent_mcp_example
2323

2424
- caption: "4. Resources"

chapter_3/agent_implementation.ipynb

Lines changed: 48 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,21 @@
55
"id": "267ac72b",
66
"metadata": {},
77
"source": [
8-
"# 3.2 Deploy agents with Anthropic's native tool use\n",
8+
"# 3.3 Deploy agents with Anthropic's native tool use\n",
99
"\n",
10-
"Now let's see how we can enhance the capabilities of LLMs with added tools. \n",
10+
"In chapter 3.2 we learned how to implement an agent from scratch using python libraries. In this chapter we'll learn how implement an agent using Anthropic's native tool use. \n",
1111
"\n",
12-
"## 3.2.1 Problem setup\n",
12+
"Compared to chapter 3.2, the imports here are minimal — since we're using Anthropic's native tool \n",
13+
"use, we don't need `ldp` or any agent framework. The only library we need is the `anthropic` Python \n",
14+
"package, which provides a straightforward client for interacting with Claude. But cost and flexibility are downsides here. \n",
1315
"\n",
14-
"The goal of this section is to build an AI agent that can analyze protein data and generate hypotheses for drug discovery.\n",
16+
"We'll use the same problem setup and tools as in chapter 3.2. The agent is built to analyze protein data and generate hypothesis for drug discovery. \n",
1517
"\n",
16-
"When you understand the basic workflow, you will be able to design and build your own agents.\n",
18+
"Tools:\n",
19+
"- `analyze_protein_sequence` — computes basic biophysical properties\n",
20+
"- `summarize_protein_role` — asks the AI to summarize biological context\n",
1721
"\n",
18-
"Let's start with installing dependencies and loading API keys below. We will be using the Anthropic native tool use in this example, so make sure you have an Anthropic API key."
22+
"Let's start with installing dependencies and loading API keys below. As we're using the Anthropic native tool use in this example, make sure you have an Anthropic API key."
1923
]
2024
},
2125
{
@@ -62,7 +66,11 @@
6266
"id": "08721371",
6367
"metadata": {},
6468
"source": [
65-
"## 3.2.2 Import libraries and set up the LLM client\n"
69+
"## 3.3.1 Import libraries and set up the LLM client\n",
70+
"\n",
71+
"We instantiate the client using our API key, which will be used for all subsequent calls to the \n",
72+
"model. Think of the client as a messenger between your Python code and Anthropic's servers. When you want Claude to do something, you don't communicate with it directly over the internet yourself. Instead, you hand your request to the client object, which handles the networking, authentication, and formatting behind the scenes, and brings the response back to you. All \n",
73+
"subsequent interactions with Claude in this chapter will go through this client."
6674
]
6775
},
6876
{
@@ -76,8 +84,7 @@
7684
"from Bio.SeqUtils.ProtParam import ProteinAnalysis \n",
7785
"\n",
7886
"# Connect to the Claude AI model.\n",
79-
"# Make sure you have set your API key as an environment variable:\n",
80-
"# export ANTHROPIC_API_KEY=\"sk-ant-...\"\n",
87+
"# Make sure you have set your API key as an environment variable\n",
8188
"client = anthropic.Anthropic(api_key=get_api_key())\n",
8289
"\n",
8390
"print(\"Libraries loaded and client ready.\")"
@@ -88,13 +95,17 @@
8895
"id": "73f7e483",
8996
"metadata": {},
9097
"source": [
91-
"## 3.2.3 Define the tools\n",
92-
"As we discussed previously, tools are Python functions our agent can choose to call. \n",
98+
"## 3.3.2 Define the tools\n",
99+
"As we discussed previously, tools are Python functions our agent can choose to call. Before deploying the agent, we need to tell Claude what tools it has access to and how to use them. \n",
93100
"\n",
94-
"We give each tool a name, a description (so the AI understands what it does), and a list of inputs it expects.\n",
95-
"In this example we'll build **two tools**:\n",
96-
"- `analyze_protein_sequence` — computes basic biophysical properties\n",
97-
"- `summarize_protein_role` — asks the AI to summarize biological context\n"
101+
"With Anthropic's native tool use, tools are defined as JSON schemas and passed directly to the API and \n",
102+
"no external libraries are required. Each tool definition includes a `name`, a `description` that helps \n",
103+
"Claude decide when to use it, and an `input_schema` that specifies what parameters the tool expects. \n",
104+
"\n",
105+
"Below we define our two tools:\n",
106+
"- `analyze_protein_sequence`: takes an amino acid sequence and returns biophysical properties like molecular weight and isoelectric point.\n",
107+
" \n",
108+
"- `summarize_protein_role`: takes a protein name and returns a plain-language summary of its biological function."
98109
]
99110
},
100111
{
@@ -105,7 +116,6 @@
105116
"outputs": [],
106117
"source": [
107118
"# ── TOOL 1: Analyze a protein sequence ──────────────────────────────────────\n",
108-
"\n",
109119
"def analyze_protein_sequence(sequence: str) -> dict:\n",
110120
" \"\"\"\n",
111121
" Takes a protein sequence (one-letter amino acid codes, e.g. 'MKTIIALSYIFCLVFA...')\n",
@@ -162,7 +172,7 @@
162172
"id": "9c082794",
163173
"metadata": {},
164174
"source": [
165-
"Now let's package these as **tool definitions**. This is a structured description the AI model can read to understand what tools are available."
175+
"After writing the python functions for the two tools, we have to format the tool definition using JSON schemas and pass directly to the API. "
166176
]
167177
},
168178
{
@@ -222,23 +232,27 @@
222232
"id": "cbeb2794",
223233
"metadata": {},
224234
"source": [
225-
"## 3.2.4 Build the agent loop\n",
235+
"## 3.3.3 Build the agent loop\n",
226236
"\n",
227-
"Here is the heart of the agent. The logic is simple:\n",
237+
"At the heart of the agent is a back-and-forth conversation between your code and Claude. This loop continues until Claude has enough information to answer the question. \n",
228238
"\n",
239+
"Unlike a standard API call where you send a prompt and get a response, tool use is inherently iterative: Claude may need to call `analyze_protein_sequence`, inspect the result, and then decide to also call `summarize_protein_role` before it can draw a conclusion. Each tool result gets added back into the conversation so Claude always has full context of what it has already looked up. The logic follows a straightforward cycle as shown below.\n",
240+
"\n",
241+
"```{figure} ../figures/agent_loop.png\n",
242+
":alt: agent_loop\n",
243+
":align: center\n",
244+
":figclass: caption-centered\n",
245+
"\n",
246+
"Figure 3.3.1: In an agent loop the LLM decides if it requires a tool, our code executes the tool, receives the result from the execution. This cycle will continue until the LLM decides to terminate the cycle. \n",
229247
"```\n",
230-
"User sends a question\n",
231-
"\n",
232-
"Claude decides if it needs a tool\n",
233-
" ↓ (if yes)\n",
234-
"We run that tool and send the result back to Claude\n",
235-
"\n",
236-
"Claude decides if it needs another tool (or is done)\n",
237-
" ↓ (when done)\n",
238-
"Claude writes its final answer\n",
239-
"```\n",
240248
"\n",
241-
"This loop continues until Claude stops calling tools."
249+
"\n",
250+
":::{admonition}\n",
251+
":class: note\n",
252+
"\n",
253+
"The LLM doesn't run the tools itself. It simply tells you *which* tool it wants to call and *what arguments* to pass. Your code is responsible for actually executing the \n",
254+
"tool and returning the result.\n",
255+
":::\n"
242256
]
243257
},
244258
{
@@ -348,9 +362,9 @@
348362
"id": "b23b22f3",
349363
"metadata": {},
350364
"source": [
351-
"## 3.2.5 Complete agent workflow\n",
365+
"## 3.3.4 Test the agent\n",
352366
"\n",
353-
"Let's try it on a classic drug target: **EGFR** (Epidermal Growth Factor Receptor), a key protein in many cancers.\n",
367+
"Now tet's try our agent on a classic drug target: **EGFR** (Epidermal Growth Factor Receptor), a key protein in many cancers.\n",
354368
"\n",
355369
"We'll provide a short fragment of its sequence so the notebook runs quickly. In a real workflow, you'd paste the full sequence from UniProt.\n",
356370
"\n",
@@ -388,7 +402,7 @@
388402
"id": "e2e87036",
389403
"metadata": {},
390404
"source": [
391-
"## 3.2.6 Recap of agent set up\n",
405+
"## 3.3.5 Recap of agent set up\n",
392406
"\n",
393407
"| Step | Agent Actions |\n",
394408
"|------|--------------------|\n",
@@ -397,9 +411,7 @@
397411
"| 3️ | Decided to call `summarize_protein_role` → got biological context |\n",
398412
"| 4️ | Combined both results → generated drug hypotheses |\n",
399413
"\n",
400-
"You can test this agent with a protein of your choice by re-running the agent with a new protein sequence. You can also add more tools such as literature searching or plotting tools to analyze your data.\n",
401-
"\n",
402-
"Note that you can use the RAG pipeline we learned in section 2.2 as a tool here to extract literature data."
414+
"Test this agent with a protein of your choice by re-running the agent with a new protein sequence. You can also add more tools such as literature searching or plotting tools to analyze your data. For example that you can use the RAG pipeline we learned in section 2.2 as a tool here to extract literature data."
403415
]
404416
}
405417
],

0 commit comments

Comments
 (0)