The agentic cycle is the pattern where the LLM repeatedly calls tools, receives results, and continues reasoning until it can provide a final answer. This enables complex multi-step operations.
Here's a real-world example of the agentic cycle:
User: "Find me flights to Paris under $500 and book the cheapest one"
Cycle 1: LLM calls searchFlights({destination: "Paris", maxPrice: 500})
Cycle 2: LLM analyzes results and calls bookFlight({flightId: "F1"})
Cycle 3: LLM generates final response
// Tool definitions
const getWeatherDef = toolDefinition({
name: "get_weather",
description: "Get current weather for a city",
inputSchema: z.object({
city: z.string(),
}),
});
const getClothingAdviceDef = toolDefinition({
name: "get_clothing_advice",
description: "Get clothing recommendations based on weather",
inputSchema: z.object({
temperature: z.number(),
conditions: z.string(),
}),
});
// Server implementations
const getWeather = getWeatherDef.server(async ({ city }) => {
const response = await fetch(`https://api.weather.com/v1/${city}`);
return await response.json();
});
const getClothingAdvice = getClothingAdviceDef.server(async ({ temperature, conditions }) => {
// Business logic for clothing recommendations
if (temperature < 50) {
return { recommendation: "Wear a warm jacket" };
}
return { recommendation: "Light clothing is fine" };
});
// Server route
export async function POST(request: Request) {
const { messages } = await request.json();
const stream = chat({
adapter: openai(),
messages,
model: "gpt-4o",
tools: [getWeather, getClothingAdvice],
});
return toStreamResponse(stream);
}
// Tool definitions
const getWeatherDef = toolDefinition({
name: "get_weather",
description: "Get current weather for a city",
inputSchema: z.object({
city: z.string(),
}),
});
const getClothingAdviceDef = toolDefinition({
name: "get_clothing_advice",
description: "Get clothing recommendations based on weather",
inputSchema: z.object({
temperature: z.number(),
conditions: z.string(),
}),
});
// Server implementations
const getWeather = getWeatherDef.server(async ({ city }) => {
const response = await fetch(`https://api.weather.com/v1/${city}`);
return await response.json();
});
const getClothingAdvice = getClothingAdviceDef.server(async ({ temperature, conditions }) => {
// Business logic for clothing recommendations
if (temperature < 50) {
return { recommendation: "Wear a warm jacket" };
}
return { recommendation: "Light clothing is fine" };
});
// Server route
export async function POST(request: Request) {
const { messages } = await request.json();
const stream = chat({
adapter: openai(),
messages,
model: "gpt-4o",
tools: [getWeather, getClothingAdvice],
});
return toStreamResponse(stream);
}
User: "What should I wear in San Francisco today?"
Agentic Cycle:
