Here is a hands-on tutorial to build your first AI Agent using Node.js, TypeScript, Groq Cloud, and the Llama 3 model.
This tutorial uses AI SDK 6 (Beta) to leverage the new ToolLoopAgent class, which automatically handles the complex execution loops required for agents.
First, create a new directory for your agent and initialize a TypeScript project.
1mkdir my-groq-agent2cd my-groq-agent3npm init -y1pnpm install -D typescript tsx @types/node2npx tsc --initYou need to install the Beta version of the AI SDK to access the new Agent capabilities, along with the Groq provider and zod for tool validation.
1pnpm install ai@beta @ai-sdk/groq zod dotenvCreate a .env file in your project root to store your API key securely.
1GROQ_API_KEY=your_groq_api_key_here(Replace your_groq_api_key_here with your actual key from Groq Cloud).
Create a file named index.ts. We will implement a ToolLoopAgent. This class encapsulates the model, instructions, and tools, automatically managing the loop where the model calls a tool, gets the result, and continues generating,.
We will use the Llama 3.3 70B model (via Groq) and give it a "Weather" tool so it can fetch external data.
1import 'dotenv/config';2import { ToolLoopAgent, tool } from 'ai';3import { groq } from '@ai-sdk/groq';4import { z } from 'zod';5
6async function main() {7 // 1. Define the Agent8 const agent = new ToolLoopAgent({9 // Use the Groq provider with Llama 3 model10 model: groq('llama-3.3-70b-versatile'),11
12 // System instructions define behavior13 instructions: 'You are a helpful assistant that retrieves weather information.',14
15 // Define tools the agent can use16 tools: {17 weather: tool({18 description: 'Get the weather in a specific location',19 inputSchema: z.object({20 location: z.string().describe('The city and state, e.g. San Francisco, CA'),21 }),22 // This execute function runs on your server23 execute: async ({ location }) => {24 console.log(`[Tool] Fetching weather for ${location}...`);25 // Simulating an API call for this tutorial26 const temp = 72 + Math.floor(Math.random() * 20) - 10;27 return {28 temperature: temp,29 condition: 'Sunny',30 location: location31 };32 },33 }),34 },35 });36
37 // 2. Run the Agent38 // The agent will automatically loop:39 // Call LLM -> Call Tool -> Feed Result to LLM -> Final Answer40 console.log('User: What is the weather in New York?');41
42 const result = await agent.generate({43 prompt: 'What is the weather in New York?',44 });45
46 // 3. Output the result47 console.log(`Agent: ${result.text}`);48}49
50main().catch(console.error);Execute the script using tsx (TypeScript Executor):
1npx tsx index.tsExpected Output:
User: What is the weather in Sydney?
[Tool] Fetching weather for Sydney...
Agent: The current weather in Sydney is 55°C and Sunny.
You can make the agent smarter by adding a second tool (e.g., a currency converter or temperature converter). The ToolLoopAgent will automatically chain them together.
Update your tools object in index.ts to include this converter:
1tools: {2 weather: tool({ /* ... existing weather tool ... */ }),3
4 celsiusConverter: tool({5 description: 'Convert Fahrenheit to Celsius',6 inputSchema: z.object({7 fahrenheit: z.number(),8 }),9 execute: async ({ fahrenheit }) => {10 const celsius = Math.round((fahrenheit - 32) * (5 / 9));11 return { celsius };12 },13 }),14 },Now, if you update the prompt to: "What is the weather in New York in Celsius?", the agent will automatically call the Weather tool first, and then the Converter tool, before giving you the final answer.