Ai Agents 3 min read

How to Build Stateful AI Agents With the Genkit Agents API

Learn how to manage multi-turn conversation state and automate tool execution loops in your full-stack applications using the new Genkit Agents API.

You can now manage complex multi-turn AI interactions without writing custom boilerplate for every conversation loop. Google’s new Genkit Agents API consolidates message history, streaming, and tool execution into a single unified interface. This simplifies the transition from basic text generation to autonomous systems that interact with your database and external services.

This update removes the manual “plumbing” required to run agents in production. Instead of building your own systems to pass context windows back and forth, you define the tools and state persistence strategy while Genkit handles the orchestration.

Managing Agent State Persistence

Maintaining reliable context is the primary challenge when you decide to add memory to AI agents. The Genkit Agents API provides two approaches to state persistence depending on your privacy requirements and architecture.

Server-managed state stores the conversation history and agent data directly on your backend. This is the default approach for multi-device experiences. When integrated with Firebase, Genkit natively uses Firestore to maintain this state across sessions. The agent retrieves the historical context from the database automatically before processing a new user prompt.

Client-managed state passes the entire conversation payload back and forth between the client and the server on every request. This approach is ideal for ephemeral sessions or applications with strict data residency constraints where the server should not persist user data. The tradeoff is increased network payload size as the conversation grows.

Automating Tool Loops

Older agent architectures required developers to manually inspect model outputs, intercept tool calls, execute the corresponding functions, and return the results to the model. Genkit abstracts this “thought-action-observation” cycle.

When a model determines it needs to use a connected tool, the Agents API automatically halts generation, executes the function on your backend, and feeds the resulting data back into the prompt context. This loop continues autonomously until the model generates a final text response for the user. Understanding how function calling works helps you design better tools for this automated cycle.

The framework provides built-in support for partial responses during these loops. You can configure your UI components to display real-time streaming text or status indicators while the agent is busy calling databases or external APIs.

Multi-Agent Orchestration

Complex workflows often fail when handled by a single monolithic prompt. The Genkit framework includes specialized templates for multi-agent orchestration, allowing you to split tasks across discrete models.

You can define a routing structure where a “Researcher” agent gathers data from an API, synthesizes the results, and hands the structured data off to a “Writer” agent for formatting. This pattern aligns with how complex multi-agent systems isolate context windows to prevent model confusion.

While Genkit’s plugin architecture remains model-agnostic, the automated tool loops and routing features are highly optimized for Gemini 1.5 Pro and Gemini 1.5 Flash. Using third-party models may require additional configuration to ensure the tool execution loops fire reliably.

Local Testing and Deployment

Testing autonomous agents requires visibility into the internal reasoning loops. The Genkit Developer UI provides a local web-based environment for inspecting these traces. You can step through individual agent executions, view the exact database payloads injected into the context window, and debug tool failures before pushing code.

When deploying, the API integrates deeply with the Firebase ecosystem. You can deploy Genkit workflows as Cloud Functions with a single command. The integration automatically binds Firebase Auth to your endpoints, ensuring that only authenticated users can trigger specific agent runs and preventing unauthorized API consumption.

Review the official Genkit repository for the updated full-stack templates to see the specific context injection methods used for dynamic database lookups.

Get Insanely Good at AI

Get Insanely Good at AI

The book for developers who want to understand how AI actually works. LLMs, prompt engineering, RAG, AI agents, and production systems.

Keep Reading

Ai Agents

Bitcoin Core discloses CVE-2026-117348, a node-stalling bug found by Spiral's Loupe LLM scanner and labeled 'easily discoverable'

One peer could freeze a Bitcoin Core node for over a dozen seconds per message by repeating an orphan transaction's ID 50,000 times. An automated Claude-and-Codex scan found it in July; fixes for 30.x and 31.x shipped on November 20.

Ai Agents

How to Control Agent Tool Execution via Genkit Middleware

Learn how to use Google's new Genkit Middleware to intercept model calls, implement human-in-the-loop tool approvals, and handle transient API failures.

Ai Agents

Amazon: a China-linked actor's Cairn agents found a MONAI Label zero-day on their own and went from API map to file write in 11 minutes

Amazon Threat Intelligence says autonomous agents told only to 'obtain shell access' found an unknown path traversal in MONAI Label and got a root shell on a university server. The fix is merged, but no patched release has shipped.

Ai Agents

European Commission confirms an 'AI-generated' cyber campaign; reports tie it to ARTEX after 10 days of HR outages

The Commission said on November 19 that it took staff applications offline after detecting an AI-driven intrusion campaign on October 22. Politico's sources say CERT-EU found the attackers using ARTEX. What is confirmed, what is not, and what the FBI and CERT-EU advisories say defenders should do.

Ai Agents

Reuters tests show Meta's Muse agent read Signal messages through Android notifications and Mac accessibility tools

A Reuters investigation with Signal and the AI Now Institute found Meta's Muse agent could read Signal chats on Android and macOS without a Signal connection, while internal posts show staff flagging phantom bank balances and unwanted actions since September.