Index of every page on this site: llms.txt
All skills
genkit-ai avatar

/developing-genkit-js

@d12c988
by Genkitgenkit-ai/skills40 stars
13
Run it yourself

Provides guidance on developing AI-powered applications using Genkit in Node.js/TypeScript. Use when the user asks about Genkit, AI agents, flows, or tools in JavaScript/TypeScript, or when encountering Genkit errors, validation issues, type errors, or API problems. Don't use for Genkit in Python, Go, or Dart.

Use this Skill: https://skilld.dev/gh/genkit-ai/skills/developing-genkit-js

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

referencesagents-multi-agent.md

≈2.1k tokens on demand. Your agent reads this file only when SKILL.md points to it.

Multi-Agent Orchestration / Sub-Agents (Beta)

Beta / preview API. Sub-agent delegation uses the agents() middleware from @genkit-ai/middleware. Read agents.md first.

A common pattern is an orchestrator agent that delegates tasks to specialized sub-agents (e.g. a researcher and a coder). The agents() middleware injects one delegation tool per sub-agent (delegate_to_<name>), appends a <sub-agents> block to the orchestrator's system prompt, and — when the model calls a delegation tool — runs the sub-agent and returns its response as the tool result.


1. Define the sub-agents

Give each sub-agent a description — it's auto-discovered and shown to the orchestrator so the model knows when to delegate.

import { artifacts, retry } from '@genkit-ai/middleware';
import { ai, defaultModel } from './genkit.js';

export const researcher = ai.defineAgent({
  name: 'researcher',
  description:
    'A thorough research assistant that searches the web and provides ' +
    'well-sourced answers.',
  model: defaultModel,
  system:
    'You are a thorough research assistant. Save findings with write_artifact.',
  maxTurns: 10,
  use: [retry(), artifacts()],
});

export const coder = ai.defineAgent({
  name: 'coder',
  description: 'An expert programmer that writes clean, well-commented code.',
  system: 'You are an expert programmer. Save code with write_artifact.',
  maxTurns: 10,
  use: [artifacts(), retry()],
});

2. Wire up the orchestrator

Add agents() to the orchestrator's use: [...]. Each entry is a name string (description auto-discovered) or { name, description } to override.

import { agents, artifacts, retry } from '@genkit-ai/middleware';
import { ai } from './genkit.js';

export const orchestratorAgent = ai.defineAgent({
  name: 'orchestratorAgent',
  system: `You are a helpful project assistant. Analyze the request and
delegate to the appropriate sub-agent. If it needs research AND code, call them
sequentially, then synthesize a final answer.`,
  use: [
    agents({
      agents: [
        'researcher', // auto-discovered description
        {
          name: 'coder',
          description: 'Writes, debugs, and explains code. Use for programming.',
        },
      ],
      maxDelegations: 5, // guard rail against runaway delegation loops
      historyLength: 4, // forward the last N user/model messages as context
      artifactStrategy: 'session', // see "Sharing artifacts" below
    }),
    artifacts({ readonly: true }), // read sub-agent artifacts via read_artifact
    retry(),
  ],
});

Run it like any other agent:

const chat = orchestratorAgent.chat();
const turn = chat.sendStream(
  'Research the best sorting algorithms, then write a TypeScript quicksort.'
);
for await (const chunk of turn.stream) process.stdout.write(chunk.text ?? '');
const res = await turn.response;

agents() options

  • agents (required): array of sub-agent refs. A string (name, description auto-discovered from the registry) or { name, description? } (explicit override).
  • toolPrefix: prefix for generated tool names. Default "delegate_to" → delegate_to_<agent>. Empty string uses bare agent names.
  • maxDelegations: max delegations per generate call. Prevents runaway loops.
  • historyLength: number of recent user/model messages forwarded to sub-agents as context. 0/omitted sends only the task description. (Only client-managed sub-agents — no store — accept ad-hoc seeded history; server-managed sub-agents skip it and just receive the task.)
  • artifactStrategy: 'inline' (default) or 'session' — see below.
  • async: enables background delegation (see below).
  • maxWaitSeconds: caps one wait_for_background_tasks call, whatever timeout the model asks for.

Background delegation (async: true)

Requires genkit >= 1.43.0.

By default a delegation blocks the orchestrator's turn. With async: true, each delegation tool accepts background: true and returns a task ID (<agent>:<snapshotId>) immediately; the orchestrator keeps working and collects results later. Three tools are added:

  • check_background_tasks({ taskIds }): current status, no waiting.
  • wait_for_background_tasks({ taskIds, timeoutSeconds?, waitFor? }): blocks until they settle (waitFor: "first" returns on the first one).
  • abort_background_tasks({ taskIds }): stops tasks no longer needed.

The sub-agent needs a store. Background work is tracked by a pending snapshot; a store-less sub-agent can only be delegated to synchronously.

import { InMemorySessionStore } from 'genkit/beta';

const logAnalyst = ai.defineAgent({
  name: 'logAnalyst',
  description: 'Reads service logs and reports what they show.',
  store: new InMemorySessionStore(), // required for background delegation
  tools: [readLogs],
});

const commander = ai.defineAgent({
  name: 'commander',
  system:
    'Start investigations IN THE BACKGROUND ("background": true), post a status ' +
    'update while they run, then collect them with wait_for_background_tasks.',
  tools: [postStatus],
  maxTurns: 20, // background work adds tool rounds; raise the ceiling
  use: [agents({ agents: ['logAnalyst', 'deployHistorian'], async: true })],
});

Tell the orchestrator in its system prompt to set "background": true; the model decides per call. History is never forwarded to a store-backed sub-agent, so task descriptions must stand alone.

Continuing a task (continue_task)

Requires genkit >= 1.43.0.

A delegation to a store-backed sub-agent returns { response, taskId, status } even when synchronous. The continue_task({ taskId, instructions?, background? }) tool picks it up:

  • failed / aborted: retries from the last saved progress (finished tool calls are not repeated); instructions steer the retry.
  • completed: follow-up instructions in the sub-agent's own session.
  • expired: fences the dead worker, then continues.
  • pending / aborting: refused; use the wait/check tool.

Task IDs are scoped to the conversation: the tools only accept IDs this conversation's delegations returned. Two agents() instances in one call need distinct toolPrefix values, or both register continue_task and the call is rejected.

Sharing artifacts between agents

Sub-agents can produce artifacts. artifactStrategy controls how they reach the orchestrator:

  • 'inline' (default): artifact content is included in the delegation tool result (so the model sees it directly) and merged into the parent session.
  • 'session': artifacts are merged into the parent session only; the tool result lists artifact names, not content. Pair with the artifacts() middleware so the orchestrator can read_artifact on demand (the pattern shown above). Merged artifacts are namespaced by an invocation ID (<agentName>_<rand>/<name>).

Other middleware

@genkit-ai/middleware also exports retry, fallback, filesystem, skills, toolApproval, and contextCompression. They attach the same way via use: [...] on an agent (or on ai.generate) — see using middleware. retry() is commonly paired with delegation:

import { retry } from '@genkit-ai/middleware';

retry({
  maxRetries: 3, // default 3
  initialDelayMs: 1000, // default 1000
  maxDelayMs: 60000, // default 60000
  backoffFactor: 2, // default 2 (exponential backoff)
  // statuses defaults to UNAVAILABLE, DEADLINE_EXCEEDED, RESOURCE_EXHAUSTED,
  // ABORTED, INTERNAL
});

Note: if a sub-agent triggers an interrupt or fails, it is reported back to the orchestrator as a normal tool response (not propagated as a resumable interrupt or thrown). Tell the orchestrator how to handle failures, e.g. with continue_task. Interactive, stateful sub-agent interrupts are a future feature, so delegate self-contained tasks.

Source: SKILL.md on GitHub

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

No alerts13d5 checks · Risk SAFE
  • Gen Agent Trust Hub14d

    The skill is a comprehensive development guide for the Genkit JS framework. It provides technical instructions, code examples, and troubleshooting steps. All referenced tools and packages are official or standard industry dependencies. No security risks were identified.

  • Socket13d

    No alerts

  • Snyk14d

    Risk: LOW · No issues

  • Runlayer8mo

    6 files scanned · No issues

  • ZeroLeaks7mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at d12c988. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 4 days ago.

Activeupdated 2 weeks ago
metadata
{
  "version": "1.0.0",
  "category": "AiAndMachineLearning"
}
  • TypeScript
  • genkit
  • nodejs
  • ai-agents
  • flows
  • llm
  • google-ai
  • openai
  • anthropic

README badge

README badge for genkit-ai/skills/developing-genkit-js

Develop AI-powered applications using Genkit in Node.js and TypeScript, including flows, tools, and multi-model integrations. Covers Genkit CLI setup, flow definition with schema validation, provider selection (Google AI, OpenAI, Anthropic, Ollama), and error troubleshooting for version 1.29.0 and later.

Generated from the current SKILL.md.

What Genkit CLI version does this skill require?
Minimum CLI version 1.29.0. Verify with `genkit --version` and upgrade with `npm install -g genkit-cli@^1.29.0` if needed.
Does this skill cover pre-1.0 Genkit or older versions?
No. Genkit went through a major breaking API change. This skill assumes v1.x (1.29.0+) and will not reliably handle pre-1.0 code or deprecated APIs.
What AI providers does this skill support?
Genkit is provider-agnostic and supports Google AI, OpenAI, Anthropic, Ollama, and others. The skill defaults to Google AI if no provider is specified.
How should I troubleshoot Genkit errors?
Always read the Common Errors reference first to identify known patterns and their fixes. Use `genkit docs:search` only if the error is not documented there.
Does this skill work with Next.js, Firebase, or Express?
Yes. The skill detects your framework via package.json and adapts the implementation to framework-specific patterns like @genkit-ai/next or @genkit-ai/firebase.

Generated from the current SKILL.md. These answers refresh after source changes.