Briefing

Genkit generateMiddleware: Intercepting LLM Calls for Resilient Agents

ai-dev
by pavelgj ·

Implement Genkit generateMiddleware to intercept model, generate, and tool calls, and use softFail, smartMaxTurns, or contextCompression to handle errors, max turns, and context size.

What to do now

Implement Genkit generateMiddleware to intercept model, generate, and tool calls, and use softFail, smartMaxTurns, or contextCompression to handle errors, max turns, and context size.

Summary

Genkit’s newly released generateMiddleware API lets developers intercept every level of an LLM inference: model calls, generate loop turns, and tool executions. The middleware receives a name, a config schema, and a handler that can modify requests and responses or short‑circuit the loop. Three example middlewares—softFail, smartMaxTurns, and contextCompression—demonstrate how to recover from 503 errors, limit turns, and reduce context size, respectively. softFail catches model, tool, and max‑turn failures and returns a GenerateResponse with finishReason 'aborted' instead of throwing. smartMaxTurns enforces a maximum turn count and can return partial results. contextCompression trims the conversation history to keep token usage within limits. The middleware is registered as a plugin and can be applied per‑call via ai.generate(..., use: [myMiddleware({…})]). The recursive nature of the generate hook allows nested middleware to wrap each turn of the loop. By exposing these hooks, Genkit removes the need for ad‑hoc wrappers and provides type‑safe, lifecycle‑aware error handling. The article outlines how this middleware pattern transforms agentic workflows into resilient, maintainable systems.

Key changes

  • generateMiddleware API exposes model, generate, and tool hooks
  • softFail middleware catches 503, tool errors, and max‑turn failures, returns aborted response
  • smartMaxTurns middleware limits turns and can return partial results
  • contextCompression middleware trims conversation history to reduce token usage
  • Middleware is registered as a plugin and applied per‑call via ai.generate(..., use: [myMiddleware({…})])
  • Recursive generate hook allows nested middleware to wrap each turn
  • Type‑safe config schemas enable per‑call configuration
  • Middleware pattern removes need for ad‑hoc wrappers and provides lifecycle‑aware error handling

Affects

internal

Customer impact

Analyzing matches…

Ask about this story

Impact on an agency? Which customers? Compare historically Risks of waiting