Genkit JS Setup
Follow these instructions to set up Genkit in the current codebase. These instructions are general-purpose and lack specific codebase knowledge, so use your best judgment when following them.
- Tell the user "I'm going to check out your workspace and set you up to use Genkit for GenAI workflows."
- If the current workspace is empty or is a starter template, your goal will be to create a simple image generation flow that allows someone to generate an image based on a prompt and selectable style. If the current workspace is not empty, you will create a simple example flow to help get the user started.
- Check to see if any Genkit provider plugin (such as
@genkit-ai/google-genaior@genkit-ai/oai-compator others, may start withgenkitx-*) is installed.- If not, ask the user which provider they want to use.
- For non-Google providers: Use
genkit docs:search "plugins"to find the correct package and installation instructions. - If they have no preference, default to
@genkit-ai/google-genaifor a quick start. - If this is a Next.js app, install
@genkit-ai/nextas well.
- Search the codebase for the exact string
genkit((remember to escape regexes properly) which indicates that the user has already set up Genkit in the codebase. If found, no need to set it up again; tell the user "Genkit is already configured in this app." and exit this workflow. - Create an
aidirectory in the primary source directory of the project (this may be e.g.srcbut is project-dependent). Adapt this path if your project uses a different structure. - Create
{sourceDir}/ai/genkit.tsand populate it using the example below. DO NOT add anextplugin to the file, ONLY add a model provider plugin to the plugins array:
import { genkit, z } from 'genkit';
// Import your chosen provider plugin here. Example:
import { googleAI } from '@genkit-ai/google-genai';
export const ai = genkit({
plugins: [
googleAI(), // Add your provider plugin here
],
model: googleAI.model('gemini-flash-latest'), // Set your provider's model here
});
export { z };
- Create
{sourceDir}/ai/toolsand{sourceDir}/ai/flowsdirectories, but leave them empty for now. - Create
{sourceDir}/ai/index.tsand populate it with the following (change the import to match import aliases intsconfig.jsonas needed):
import './genkit.js';
// import each created flow, tool, etc. here for use in the Genkit Dev UI
- Add the
genkit-clito the project'sdevDependencies(e.g.npm install -D genkit-cli, or the pnpm/yarn/bun equivalent) so the CLI version is pinned per-project and works in CI, then add agenkit:devscript topackage.jsonthat runsgenkit start -- npx tsx --watch {sourceDir}/ai/index.ts. This is the recommended way to run the app during development:genkit startwraps your program and captures a trace of every Genkit action (prompts, model I/O, tool calls) while serving the Dev UI at http://localhost:4000. Running the app directly (tsx/node/npm start) skips trace capture, so you're debugging blind. DO NOT try to run the script now. - Tell the user "Genkit is now configured and ready for use." Let them know they can start developing with
npm run genkit:dev(or the pnpm/yarn/bun equivalent), which stays running until stopped with Ctrl+C and serves the Dev UI at http://localhost:4000. Also remind them to set appropriate env variables (e.g.GEMINI_API_KEYfor Google providers). Wait for the user to prompt further before creating any specific flows.
Next Steps & Troubleshooting
- Documentation: Use the CLI to access documentation (e.g.,
genkit docs:search). - Building Flows: See examples.md for patterns on creating flows, adding tools, and advanced configuration.
- Running & Verifying:
npm run genkit:devis the default dev loop (persistent, serves the Dev UI). For a quick, non-interactive check that exits on its own, run a single flow withgenkit flow:run <flow> '<input>' -- npx tsx {sourceDir}/ai/index.tsand inspect it withgenkit trace:get <id>. See docs-and-cli.md. - Troubleshooting: If you encounter issues during setup or initialization, check common-errors.md for solutions.