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.

referencesobservability.md

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

Observability (Tracing, OpenTelemetry, Reflection API)

Pluggable instrumentation, @genkit-ai/otel, and the reflection API settings require genkit >= 1.43.0.


How tracing works

  • In dev (genkit start), Genkit posts spans straight to the Developer UI's telemetry server. No OpenTelemetry SDK is involved. Logs written with logger from genkit/logging show up next to the span that produced them.
  • Elsewhere, spans go through pluggable Instrumentation providers. The default provider creates OpenTelemetry spans with genkit:* attributes. Nothing is exported until an OTel SDK is started, directly or by a plugin such as enableFirebaseTelemetry() (Genkit Monitoring).

Export OpenTelemetry GenAI semantic conventions (@genkit-ai/otel)

Use this to send Genkit telemetry to any OTLP backend (Jaeger, Grafana, Honeycomb, Datadog, ...) in the vendor-neutral gen_ai.* shape.

npm i @genkit-ai/otel @opentelemetry/sdk-node
import { googleAI } from '@genkit-ai/google-genai';
import { GenAiInstrumentation } from '@genkit-ai/otel';
import { NodeSDK } from '@opentelemetry/sdk-node';
import { genkit } from 'genkit';
import { configureInstrumentation } from 'genkit/tracing';

// 1. The app owns the OTel SDK. With no args it reads the standard OTEL_* env
//    vars (default: OTLP http/protobuf on localhost:4318).
const sdk = new NodeSDK();
sdk.start();

// 2. Route Genkit spans through the GenAI provider. Call once, before genkit().
configureInstrumentation(
  new GenAiInstrumentation({
    contentCapturingMode: 'SPAN_ONLY', // prompts/responses on spans; may contain PII
    emitToolSpans: true, // execute_tool spans
  })
);

// 3. Use Genkit as usual.
const ai = genkit({ plugins: [googleAI()] });

What you get: chat <model> client spans with gen_ai.request.*, gen_ai.usage.*, and gen_ai.response.finish_reasons; optional execute_tool <name> spans; a generic span per other Genkit action (tagged genkit.action.type); and the gen_ai.client.token.usage and gen_ai.client.operation.duration metrics.

Options: contentCapturingMode ('NO_CONTENT' default, 'SPAN_ONLY', 'EVENT_ONLY', 'SPAN_AND_EVENT'; falls back to the OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT env var), captureActionIO (raw genkit.input/genkit.output, default false), emitToolSpans (default false), emitMetrics (default true), scopeName, and explicit tracer/meter/logger.

Gotchas:

  • Do not combine with enableFirebaseTelemetry() or enableGoogleCloudTelemetry(). configureInstrumentation replaces the default provider, and those plugins build their metrics/logs from its genkit:* attributes. Their Genkit metrics stop with no error. Pick one.
  • EVENT_ONLY content goes to the OTel logs signal, not the span. Jaeger can't show it; use SPAN_ONLY/SPAN_AND_EVENT there.
  • The Dev UI keeps working. Under genkit start, its own provider always runs first. To preview what the OTel export looks like in the Dev UI, run genkit start --experimental-use-otel -- <command>.
  • No SDK, no export. Without a started OTel SDK the provider is inert and logs one warning.

Custom instrumentation

Implement Instrumentation to send spans somewhere else. runInNewSpan wraps the operation like middleware: call next exactly once, return its result, rethrow its errors unchanged.

import { INVALID_SPAN_CONTEXT, trace } from '@opentelemetry/api';
import {
  configureInstrumentation,
  type Instrumentation,
  type InstrumentationNext,
  type InstrumentationSpanInfo,
} from 'genkit/tracing';

class TimingInstrumentation implements Instrumentation {
  async runInNewSpan<T>(
    info: InstrumentationSpanInfo,
    next: InstrumentationNext<T>
  ): Promise<T> {
    const start = performance.now();
    try {
      // Not OTel-based: pass a non-recording span and empty ids.
      return await next(trace.wrapSpanContext(INVALID_SPAN_CONTEXT), {
        traceId: '',
        spanId: '',
        setMetadata() {},
      });
    } finally {
      // metadata.state is 'success' | 'error' once next settles.
      console.log(info.metadata.path, info.metadata.state, performance.now() - start);
    }
  }
}

configureInstrumentation(new TimingInstrumentation());

Reflection API outside dev

The CLI and Dev UI talk to the app through the reflection API. By default it runs only under GENKIT_ENV=dev on 127.0.0.1:3100+. To run it elsewhere (e.g. staging), turn it on explicitly and protect it:

GENKIT_REFLECTION_ENABLED=true \
GENKIT_REFLECTION_HOST=0.0.0.0 \
GENKIT_REFLECTION_PORT=3100 \
GENKIT_REFLECTION_SECRET_TOKEN="$(openssl rand -hex 32)" \
node lib/index.js
  • GENKIT_REFLECTION_ENABLED: true on anywhere, false off even under dev. Any other value makes genkit() throw.
  • GENKIT_REFLECTION_SECRET_TOKEN: every request must send it in the x-genkit-reflection-secret header (/api/__health stays open). Without it, anyone who can reach the port can run every flow, model, and tool.
  • genkit({ reflectionPort: 3200 }) binds exactly that port and fails if taken; 0 lets the OS pick; unset searches upward from 3100. The env var wins over the option, and neither turns the API on.

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.