Agents (Experimental)
Experimental APIs live under
.expmodules (genkit.exp,genkit.exp.agent,genkit_fastapi.exp,genkit_middleware.exp,genkit_google_cloud.exp) and can change between minor releases.
Turn a model and tools into a stateful multi-turn conversation with snapshots, typed custom state, human approvals, branching, and detached background runs.
Deeper topics: sessions · HITL · branching · background · state · artifacts · custom · HTTP
- Define an agent: Lines 26-85
- Tools, progress chunks, and multipart responses: Lines 87-117
- Middleware: Lines 119-169
- Chat and streaming: Lines 171-206
- Errors and cancellation: Lines 208-247
- Without a store: Lines 249-271
- Verify an agent from the CLI (flow:run): Lines 273-290
Define an agent
Import Genkit from genkit.exp to opt into define_agent,
define_prompt_agent, define_custom_agent, and ai.agent(name). Stable
from genkit import Genkit does not expose agent methods.
from genkit_google_genai import GoogleAI
from pydantic import BaseModel
from genkit.exp import Genkit
from genkit.exp.agent import InMemorySessionStore
class WeatherInput(BaseModel):
location: str
class WeatherOutput(BaseModel):
weather: str
temperature: str
# 1. Initialize experimental Genkit instance
ai = Genkit(plugins=[GoogleAI()])
# 2. Define a typed tool
@ai.tool(name='getWeather', description='Get weather for a city.')
async def get_weather(input: WeatherInput) -> WeatherOutput:
return WeatherOutput(weather=f'Sunny in {input.location}', temperature='21°C')
# 3. Register a store-backed agent
agent = ai.define_agent(
name='weatherAgent',
model=GoogleAI.gemini('gemini-flash-latest'),
system='Weather assistant. Use getWeather for weather questions.',
tools=[get_weather],
store=InMemorySessionStore(), # omit store= for client-managed history
)Key define_agent keyword arguments:
model:GoogleAI.gemini('gemini-flash-latest')(or a'plugin/model'string)config: typed config such asGeminiConfig(thinking_level='HIGH')orAnthropicConfig(thinking='adaptive', effort='high')use: middleware instances (Retry,Fallback,ToolApproval,Filesystem,Skills,Artifacts,Surfaces)state_schema: PydanticBaseModeltypingchat.state,res.state, andchunk.custom(state)max_turns: tool-loop ceiling per user message (default 50)state_transform/chunk_transform: egress hooks that redact client views without altering stored state
For .prompt files or ai.define_prompt(...), wire the registered prompt name
directly with ai.define_prompt_agent(name='greeterPrompt', store=...). For
custom turn loops, use ai.define_custom_agent(...).
Tools, progress chunks, and multipart responses
A tool takes at most one typed input parameter (a Pydantic BaseModel for
structured arguments or a scalar like str, or no input parameter) plus an
optional ToolContext[ChunkT] parameter.
import asyncio
from pydantic import BaseModel
from genkit import MultipartToolResponse, Part, ToolContext
class PrepProgress(BaseModel):
stage: str
percent: int
@ai.tool(name='prepareOrder', description='Prepare a kitchen order and stream stage progress.')
async def prepare_order(dish: str, ctx: ToolContext[PrepProgress]) -> MultipartToolResponse[dict[str, str]]:
# 1. Stream partial tool progress (arrives on chunk.tool_responses with part.is_partial == True)
ctx.send_chunk(PrepProgress(stage='searing', percent=40))
await asyncio.sleep(0.2)
ctx.send_chunk(PrepProgress(stage='plating', percent=90))
# 2. Return structured output alongside extra content parts (e.g. media or text)
return MultipartToolResponse(
output={'status': 'ready', 'dish': dish},
content=[Part.from_text(f'Ticket closed for {dish}.')],
)Middleware
Import standard middleware from genkit_middleware, session-bound Artifacts
from genkit_middleware.exp, and generative UI Surfaces from genkit_a2ui.
Register Middleware() in plugins=[...] when you want standard middleware
listed in the Dev UI or referenced by .prompt files; passing instances in
use=[...] works either way.
Filesystem(genkit_middleware):list_filesandread_filescoped toroot_dir; passallow_write_access=Trueto addwrite_fileandedit_file.ToolApproval(genkit_middleware): tools inallowed_toolsrun automatically; all other tools pause the turn with an interrupt (HITL). It gates every tool call, including the onesFilesystemandArtifactsadd.Skills(genkit_middleware): injectsSKILL.mddescriptions and exposesuse_skill.Retry/Fallback(genkit_middleware):Retryretries transient failures (UNAVAILABLE,DEADLINE_EXCEEDED,RESOURCE_EXHAUSTED,ABORTED,INTERNALby default);Fallbackswitches to alternate models.Artifacts(genkit_middleware.exp): addsread_artifactandwrite_artifacttools plus an active artifact listing in the system prompt (artifacts).Surfaces(genkit_a2ui): A2UI surface instructions, validation, and part parsing.
from genkit_google_genai import GoogleAI
from genkit_middleware import Filesystem, Middleware, Retry, Skills, ToolApproval
from genkit_middleware.exp import Artifacts
from genkit.exp import Genkit
from genkit.exp.agent import FileSessionStore
ai = Genkit(plugins=[GoogleAI(), Middleware()])
coding_agent = ai.define_agent(
name='codingAgent',
model=GoogleAI.gemini('gemini-flash-latest'),
system='Coding assistant. Explore with list_files/read_file before editing.',
use=[
ToolApproval(allowed_tools=['list_files', 'read_file', 'read_artifact', 'use_skill']),
Filesystem(root_dir='./workspace', allow_write_access=True),
Artifacts(),
Skills(skill_paths=['./skills']),
Retry(max_retries=3),
],
store=FileSessionStore('./.snapshots'),
max_turns=30,
)Chat and streaming
Run one turn at a time per AgentChat. Use ai.run_main(main()) for standalone
scripts.
async def main() -> None:
chat = agent.chat()
# 1. One-shot turn
res = await chat.send('Weather in Tokyo?')
print(res.text, res.snapshot_id)
# => It is 21°C and sunny in Tokyo. 8f3c...
# 2. Streamed turn (.stream yields AgentChunk, .response resolves to AgentResponse)
turn = chat.send_stream('What about Paris?')
async for chunk in turn.stream:
if chunk.text:
print(chunk.accumulated_text, end='\r', flush=True)
final = await turn.response
# 3. Rehydrate history from the store on a new chat instance
resumed = await agent.load_chat(snapshot_id=final.snapshot_id)
followup = await resumed.send('Which cities did we check?')
print(followup.text)
if __name__ == '__main__':
ai.run_main(main())await agent.load_chat(snapshot_id=...): fetches the stored snapshot immediately sochat.messages,chat.state, andchat.artifactsare populated locally.agent.chat(session_id=...): attaches a lazy resume handle that continues from the session's last completed turn, skipping any failed or aborted leaf.
Errors and cancellation
Genkit separates turn failures from call failures:
- Turn failure (
res.error): When an agent turn starts and fails at runtime,chat.send(...)does not raise. It returnsAgentResponsewithres.finish_reason == AgentFinishReason.FAILED,res.text == '', andres.error(Failurewithstatus,message,details, andreason: ErrorReason). The chat drops the unanswered user prompt fromchat.messages, and the nextsend()continues from the last completed snapshot. - No turn result (
GenkitError): When no turn runs at all (dropped HTTP connection, orAgentInitErrorfrom passingsnapshot_idto a storeless agent),chat.send(...)raisesGenkitErrorand restores local chat state. Mismatched init arguments (astateblob to a store-backed agent, ormessages/statemixed with a resume id) raiseAgentInitErrorfromagent.chat(...)itself. - Cancellation: native
asyncio.CancelledError; put tool cleanup intry/finally.await turn.abort()detaches the client stream only. To cancel a running turn server-side, start it withchat.detach(...)and callawait task.abort();chat.abort()during a foregroundsend()does not stop that turn (background).
from genkit import ErrorReason, GenkitError
from genkit.exp.agent import AgentFinishReason
try:
res = await chat.send('Run portfolio rebalance.')
except GenkitError as err:
# No turn ran (transport drop or invalid init)
print(err.status, err.message)
else:
if res.error:
# Turn ran and failed; res.finish_reason == AgentFinishReason.FAILED
assert res.finish_reason == AgentFinishReason.FAILED
if res.error.reason == ErrorReason.INVALID_OUTPUT:
print('Truncated structured output:', res.error.message)Without a store
Omit store= when the client owns conversation state. snapshot_id stays
None; round-trip messages, state, and artifacts into the next
agent.chat(...) call:
stateless = ai.define_agent(
name='echoNoStore',
model=GoogleAI.gemini('gemini-flash-latest'),
system='Answer briefly and remember context.',
)
chat = stateless.chat()
await chat.send('My name is Ada.')
resumed = stateless.chat(
messages=chat.messages,
state=chat.state,
artifacts=chat.artifacts,
)
await resumed.send('What is my name? One word.')Verify an agent from the CLI (flow:run)
genkit flow:run invokes flows, not agents directly. Wrap a turn in a small
flow and propagate res.error so failures surface cleanly:
from genkit import GenkitError
@ai.flow()
async def try_weather_agent(message: str) -> str:
res = await agent.chat().send(message)
if res.error:
raise GenkitError(status=res.error.status, message=res.error.message, details=res.error.details)
return res.text
# genkit flow:run try_weather_agent '"Weather in Tokyo?"' -- uv run src/main.py