Deploy Genkit Apps on Cloudflare Workers Easily

Introduction

Genkit is an open-source framework developed by Google for creating AI-powered applications. This framework enables structured outputs, tool calling, retrieval-augmented generation (RAG), streaming, and generative UIs. It also provides utilities for tracing and evaluating AI applications, along with a user interface for testing and monitoring.

Deployment on Cloudflare Workers

Cloudflare Workers is a serverless platform that executes JavaScript code close to users, eliminating cold start issues common in other serverless environments. However, deploying Genkit applications on Workers presents certain challenges due to runtime restrictions.

Prerequisites

To get started, you need:

  • Working knowledge of TypeScript
  • Basic familiarity with Genkit, including flows and the ai.generate() method
  • A Cloudflare account
  • A Gemini API key (a Groq key for fallback is optional)
  • pnpm installed
  • curl or Postman for testing

Why Genkit Breaks on Cloudflare Workers

JavaScript needs a specific environment to run, such as the browser or Node.js, while Cloudflare Workers utilize their own runtime (workerd) based on V8. This presents issues for Genkit, which relies on functionalities that the Workers runtime restricts, including dynamic code generation with eval and new Function().

Key Restrictions and Fixes

The following fixes are necessary:

  • Utilize schema validation through interpretation rather than compilation by turning off the schema compiler with jsonSchemaMode: "interpret".
  • Pass prompts as arrays of message parts instead of strings to avoid using the dotprompt library, which calls new Function().
  • Implement an Express stub to prevent crashes during the loading of the Genkit module, as the actual express module cannot be loaded in the Workers environment.

Setup Instructions

To set up a new project, clone the starter repository and install the dependencies. Configure the project as a solid foundation to build the desired AI application.

git clone https://github.com/orimdominic/genkit-apps-on-cloudflare-workers
cd genkit-apps-on-cloudflare-workers
pnpm install

Building the Application

After setting the starter repo, install the Genkit dependencies, configure runtime settings, and implement the application logic. Specifically, implement a flow that explains technical topics in relatable terms for junior software engineers.

Local Testing and Deployment

Run the application locally to ensure the expected behavior before deploying to Cloudflare Workers. After everything is confirmed to be functioning properly, deploy the worker and configure your API keys as secrets:

pnpm run deploy
pnpm dlx wrangler secret put GEMINI_API_KEY

Limitations

When deploying Genkit on Cloudflare Workers, keep in mind:

  • The Developer UI and reflection server cannot operate in the Workers runtime.
  • Prompts must be formatted correctly to avoid runtime errors.
  • Dependencies based on Express may not function due to the use of a stub.

Conclusion

This guide provides a comprehensive overview of deploying Genkit applications on Cloudflare Workers, focusing on overcoming environment-specific challenges and ensuring proper configurations.

Source: www.freecodecamp.org

Related Articles

Overview The Agent Learning Kit, developed by Future AGI, is a local-first software development kit (SDK) and command-line interface (CLI) designed...
OpenAI has reversed its decision to cut off access to its AI models for Grok Build, formerly known as Cursor,...
Overview of Invoicing Software in 2027 As businesses evolve, so too do their invoicing practices. The best invoicing software in 2027...