Skip to content

Implementing agentic patterns

Building powerful AI systems involves more than just calling a model; it requires structuring interactions in a way that balances reliability with flexibility. This is the core idea behind the agentic scale.

At one end of the scale, you have Workflows: structured, predictable sequences of tasks. They are highly reliable but less flexible. At the other end, you have Agents: autonomous systems that can reason, plan, and use tools to handle complex, unpredictable tasks. They are highly flexible but can be less reliable.

The key to building effective AI is to find the right point on this scale for your use case, often creating a hybrid that combines the best of both worlds. This guide explores key patterns along the agentic scale and shows you how to implement them using Genkit’s core primitives like flows, tools, and interrupts.

All of the code samples in this guide can be found in the agentic-patterns sample on GitHub.

We will cover the following patterns, moving from more structured workflows to more autonomous agents:

  • Sequential Processing: The simplest workflow, decomposing a task into a fixed sequence of LLM calls.
  • Conditional Routing: Adding branching logic to a workflow based on an LLM’s output.
  • Parallel Execution: Running multiple LLM calls concurrently for speed or to gather diverse perspectives.
  • Tool Calling: Introducing flexibility by allowing an LLM to call external functions to retrieve information or perform actions.
  • Iterative Refinement: Creating a feedback loop where an LLM critiques and improves its own work.
  • Autonomous Operation: Building agents that can independently plan and execute tasks to achieve a goal.
  • Stateful Interactions: Turning any workflow into a stateful, conversational experience by managing history.

This is the simplest workflow pattern, where a task is broken down into a fixed sequence of steps. Each step processes the output of the previous one. Genkit flows are the ideal tool for orchestrating these sequences.

A key advantage of this pattern is the ability to use different models for different steps. For example, you could use a fast, cheaper model to generate an initial idea, and then a more powerful model to elaborate on it. You can also create multi-modal scenarios, like using one model to generate a text prompt for an image generation model.

In this example, the flow first generates a story idea and then uses that idea to write the beginning of the story.

import { z } from 'genkit';
import { ai } from './genkit.js';
export const storyWriterFlow = ai.defineFlow(
{
name: 'storyWriterFlow',
inputSchema: z.object({ topic: z.string() }),
outputSchema: z.string(),
},
async ({ topic }) => {
// Step 1: Generate a creative story idea
const ideaResponse = await ai.generate({
prompt: `Generate a unique story idea about a ${topic}.`,
output: {
schema: z.object({
idea: z.string().describe('A short, compelling story concept'),
}),
},
});
const storyIdea = ideaResponse.output?.idea;
if (!storyIdea) {
throw new Error('Failed to generate a story idea.');
}
// Step 2: Use the idea to write the beginning of the story
const storyResponse = await ai.generate({
prompt: `Write the opening paragraph for a story based on this idea: ${storyIdea}`,
});
return storyResponse.text;
},
);

This pattern adds branching logic to a workflow. An initial LLM call classifies the input, and the flow then routes the task to a specialized downstream path.

This is a great place to optimize for cost and latency. The initial classification step can often be handled by a smaller, faster model (like gemini-flash-latest or even gemini-flash-lite-latest), while the more complex downstream tasks can be routed to more powerful models.

This flow determines if a user’s request is a simple question or a request for creative writing and handles it accordingly.

import { z } from 'genkit';
import { ai } from './genkit.js';
export const routerFlow = ai.defineFlow(
{
name: 'routerFlow',
inputSchema: z.object({ query: z.string() }),
outputSchema: z.string(),
},
async ({ query }) => {
// Step 1: Classify the user's intent
const intentResponse = await ai.generate({
prompt: `Classify the user's query as either a 'question' or a 'creative' request. Query: "${query}"`,
output: {
schema: z.object({
intent: z.enum(['question', 'creative']),
}),
},
});
const intent = intentResponse.output?.intent;
// Step 2: Route based on the intent
if (intent === 'question') {
// Handle as a straightforward question
const answerResponse = await ai.generate({
prompt: `Answer the following question: ${query}`,
});
return answerResponse.text;
} else if (intent === 'creative') {
// Handle as a creative writing prompt
const creativeResponse = await ai.generate({
prompt: `Write a short poem about: ${query}`,
});
return creativeResponse.text;
} else {
return "Sorry, I couldn't determine how to handle your request.";
}
},
);

This pattern executes multiple LLM calls simultaneously, either to perform independent sub-tasks faster (Sectioning) or to generate multiple diverse outputs for comparison (Voting). A flow is a good place to fan the calls out and join their results.

This example uses sectioning to generate a product name and a marketing tagline at the same time.

import { z } from 'genkit';
import { ai } from './genkit.js';
export const marketingCopyFlow = ai.defineFlow(
{
name: 'marketingCopyFlow',
inputSchema: z.object({ product: z.string() }),
outputSchema: z.object({
name: z.string(),
tagline: z.string(),
}),
},
async ({ product }) => {
const [nameResponse, taglineResponse] = await Promise.all([
// Task 1: Generate a creative name
ai.generate({
prompt: `Generate a creative name for a new product: ${product}.`,
}),
// Task 2: Generate a catchy tagline
ai.generate({
prompt: `Generate a catchy tagline for a new product: ${product}.`,
}),
]);
return {
name: nameResponse.text,
tagline: taglineResponse.text,
};
},
);

This is where workflows start becoming more agentic. Instead of following a fixed path, the LLM can dynamically decide to call external functions (tools) to retrieve information or perform actions. This allows the workflow to interact with the outside world.

This flow provides an LLM with a getWeather tool. The LLM can then decide whether to call this tool based on the user’s prompt.

import { z } from 'genkit';
import { ai } from './genkit.js';
// Define a tool that can be called by the LLM
const getWeather = ai.defineTool(
{
name: 'getWeather',
description: 'Get the current weather in a given location.',
inputSchema: z.object({ location: z.string() }),
outputSchema: z.string(),
},
async ({ location }) => {
// In a real app, you would call a weather API here.
return `The weather in ${location} is 75°F and sunny.`;
},
);
export const toolCallingFlow = ai.defineFlow(
{
name: 'toolCallingFlow',
inputSchema: z.object({ prompt: z.string() }),
outputSchema: z.string(),
},
async ({ prompt }) => {
const response = await ai.generate({
prompt: prompt,
tools: [getWeather],
});
return response.text;
},
);

This pattern creates a feedback loop to improve output quality. An “optimizer” LLM generates content, and an “evaluator” LLM provides critiques. The process repeats until the output meets a desired standard, moving further toward agent-like behavior.

This flow writes a short blog post, then repeatedly evaluates and refines it until the evaluator is satisfied.

import { z } from 'genkit';
import { ai } from './genkit.js';
export const iterativeRefinementFlow = ai.defineFlow(
{
name: 'iterativeRefinementFlow',
inputSchema: z.object({ topic: z.string() }),
outputSchema: z.string(),
},
async ({ topic }) => {
let content = '';
let feedback = '';
let attempts = 0;
// Step 1: Generate the initial draft
content = (
await ai.generate({
prompt: `Write a short, single-paragraph blog post about: ${topic}.`,
})
).text;
// Step 2: Iteratively refine the content
while (attempts < 3) {
attempts++;
// The "Evaluator" provides feedback
const evaluationResponse = await ai.generate({
prompt: `Critique the following blog post. Is it clear, concise, and engaging? Provide specific feedback for improvement. Post: "${content}"`,
output: {
schema: z.object({
critique: z.string(),
satisfied: z.boolean(),
}),
},
});
const evaluation = evaluationResponse.output;
if (!evaluation) {
throw new Error('Failed to evaluate content.');
}
if (evaluation.satisfied) {
break; // Exit loop if content is good enough