🎙️ Episode 29006:27 • May 29, 2026
Claude Tool Use in TypeScript: Agentic Loop Tutorial
Listen to this episode
AI-generated discussion by Alex and Jamie
About this episode
Join hosts Alex and Jamie in this episode of the Nerd Level Tech AI Cast as they explore the exciting world of agentic loops in Claude Sonnet 4.6. Discover how to seamlessly integrate external tools using TypeScript and the Anthropic SDK, all while enjoying some light-hearted banter about coffee and coding mishaps. Whether you're a seasoned developer or just curious about AI, this conversation offers valuable insights into building responsive and engaging AI interactions.
Transcript
[Alex]: Welcome back to the Nerd Level Tech AI Cast, where the only thing loopier than our agentic code is Jamie’s coffee intake. [Jamie]: Hey, I only had three cups today. That’s, like, a low-traffic day for my neurons. [laughs] Anyway, I’m Jamie. [Alex]: And I’m Alex. Today, we’re diving into something that’s both cutting-edge and, honestly, kind of fun to play with: tool use in Claude Sonnet 4.6—specifically, how to build a real agentic loop in TypeScript using the Anthropic SDK. [Jamie]: Agentic loop? That sounds like something you get stuck in at the DMV. [Alex]: [chuckles] Not far off. Picture this: Claude, the AI model, wants to answer your questions, but sometimes it needs to call external tools—like, say, a weather function or a clock—before it can give you a solid answer. The “loop” part is how your code keeps the conversation going until Claude’s satisfied. [Jamie]: So it’s like Claude says, “Hold up, let me check the weather,” and then waits for our script to run that tool before it goes on? [Alex]: Exactly! It’s a request-response dance. And today, we’re showing how to code that dance in TypeScript, with a little help from Zod for validation, and the Anthropic SDK for talking to Claude. [Jamie]: I love it. So, what do folks need to follow along? [Alex]: First up, Node.js 24 LTS—think the latest stable Node, though Node 22 will work if you’re stubborn. You’ll need an Anthropic API key, and, honestly, just a few cents of API budget. Running the whole thing costs less than a quarter at Sonnet 4.6’s rates. [Jamie]: Cheaper than my third cup of coffee. Not bad. [PAUSE] Okay, so let’s break it down. How do you actually set this up? [Alex]: Step one: scaffold your project. Make a folder, `npm init`, install the exact versions of the anthropic-aisdk and zod. Pin those versions—Anthropic’s SDK updates a lot, and you don’t want tomorrow’s breaking change ruining your day. [Jamie]: I have learned that the hard way. Nothing like a surprise SDK update to ruin a Friday. [Alex]: Right? Once you’ve got your dependencies, drop in a strict `tsconfig.json`. Two flags worth highlighting: `noUncheckedIndexedAccess` to help TypeScript catch missing fields, and `verbatimModuleSyntax` to keep your imports clean and happy on Node 24. [Jamie]: Okay, so the foundation is set. What’s next? [Alex]: Now you define your tools. For example, a weather tool. Each tool has a name, a verbose description—seriously, Anthropic recommends three or four sentences for anything more complex than “hello world”—and a JSON Schema input. [Jamie]: Wait, why the long descriptions? Does Claude get bored otherwise? [Alex]: [laughs] Not bored, but more like… easily confused. The tool descriptions help Claude decide when to use which tool. Think of them like a README for your AI. [Jamie]: Good documentation saves the day—even for robots. [Alex]: Couldn’t have said it better. So, for the weather tool, we use Zod to validate the input—say, city and unit. If Claude tries to pass something weird, Zod catches it and your code can send back an error. [Jamie]: So Zod is like the bouncer at the club, making sure only the right folks get in? [Alex]: Exactly! And if someone with a fake ID shows up—like, say, Claude tries to pass in “rainbow” as a temperature unit—Zod is there to bounce them right out. [PAUSE] [Jamie]: Love it. All right, so we’ve got a weather tool. How does the “agentic loop” actually work? [Alex]: Here’s where it gets fun. Your main loop sends a message to Claude—like, “What’s the weather in Tokyo?” Claude either answers directly or, more likely, replies with a “tool_use” block saying, “I want to call get_weather with these inputs.” [Jamie]: So Claude basically hands us a to-do list. [Alex]: Yep. Our script runs the tool, gets the result, and sends it back in a “tool_result” block. Rinse and repeat until Claude’s stop_reason is “end_turn”—meaning, it’s done. [Jamie]: What if Claude wants to use more than one tool at once? Like, “What’s the weather in Paris and the time in Tokyo?” [Alex]: Great question! That’s parallel tool use, and Claude 4 models do this by default. If the answer needs both tools, you get two tool_use blocks in one response. Your code needs to handle them both, run each tool, and send back their results—together, in one go. [Jamie]: Wait, together? Is there a catch? [Alex]: There is. The tool_result blocks have to come first in your user message, before any text. If you mess that up—say, sneak in a text block before the tool_results—Claude throws a 400 error and you feel silly. [Jamie]: Ask me how I know… [laughs] [PAUSE] So, to recap: Claude asks for tools, we run them, send back the results, and the loop keeps going until Claude is satisfied? [Alex]: That’s it. And to make the parallelism visible, add a second tool—like a “get_time” tool that returns the local time for a given timezone. Ask Claude, “What time is it in Tokyo and what’s the weather in Paris?” and watch both tools fire off in parallel. [Jamie]: Does it ever get stuck in an endless loop? [Alex]: In theory, yes. So we put in a turn cap—say, 10 turns—just in case Claude goes existential and keeps asking for more data. [Jamie]: “I need to know the humidity in every city on Earth!” [both laugh] [Alex]: If you hit weird stop reasons like “max_tokens” or “pause_turn,” your loop can catch those and either retry or bail gracefully. [Jamie]: Okay, last question: What happens if Claude gives us bad input—like, “What’s the weather in Atlantis?” [Alex]: Zod will throw an error, your code marks the tool_result as an error, and Claude will decide what to do—maybe ask for clarification, maybe give up. It’s actually pretty elegant. [Jamie]: I love it. So you get a robust, typed, parallel-capable agent loop, all in about 140 lines of TypeScript. [Alex]: And you get to feel like you’re building your own Jarvis. Just with more type safety. [Jamie]: And fewer explosions. [Alex]: Depends on your error handling. [Jamie]: [laughs] All right, that’s a wrap for today’s Nerd Level Tech AI Cast. If you try this out, let us know what weird or wonderful tools you wire up to Claude. [Alex]: Thanks for joining us! Don’t forget to subscribe, leave a review, and keep your SDKs pinned. Catch you next time! [Outro Music Fades Out]