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

/developing-genkit-go

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

Provides guidance on developing AI-powered applications using Genkit in Go. Use when the user asks to build AI features, agents, flows, or tools in Go using Genkit, or when working with Genkit Go code involving generation, prompts, streaming, tool calling, or model providers. Don't use for Genkit in JavaScript/TypeScript, Python, or Dart.

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

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.5k tokens on demand. Your agent reads this file only when SKILL.md points to it.

Multi-Agent Orchestration / Sub-Agents (Experimental)

Experimental / preview API. Sub-agent delegation uses the Agents middleware from plugins/middleware/exp. 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 middlewarex.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 with the task and returns its response as the tool result.

import (
	aix "github.com/firebase/genkit/go/ai/exp"
	middlewarex "github.com/firebase/genkit/go/plugins/middleware/exp"
	"github.com/firebase/genkit/go/plugins/middleware" // Retry, etc.
)

1. Define the sub-agents

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

researcher := genkitx.DefineAgent(g, "researcher",
	aix.InlinePrompt{
		ai.WithModelName("googleai/gemini-flash-latest"),
		ai.WithSystem("You are a thorough research assistant. Save findings with write_artifact."),
		ai.WithUse(&middleware.Retry{}, &middlewarex.Artifacts{}),
	},
	aix.WithDescription[any]("A thorough research assistant that provides well-sourced answers."),
)

coder := genkitx.DefineAgent(g, "coder",
	aix.InlinePrompt{
		ai.WithModelName("googleai/gemini-flash-latest"),
		ai.WithSystem("You are an expert programmer. Save code with write_artifact."),
		ai.WithUse(&middlewarex.Artifacts{}, &middleware.Retry{}),
	},
	aix.WithDescription[any]("An expert programmer that writes clean, well-commented code."),
)

2. Wire up the orchestrator

Add &middlewarex.Agents{...} to the orchestrator's ai.WithUse. Reference each sub-agent by aix.AgentRef — a bare {Name: ...} (description auto-discovered from the registry) or agent.Ref() (captures the agent's name and description).

orchestrator := genkitx.DefineAgent(g, "orchestrator",
	aix.InlinePrompt{
		ai.WithModelName("googleai/gemini-flash-latest"),
		ai.WithSystem(`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.`),
		ai.WithUse(
			&middlewarex.Agents{
				Agents: []aix.AgentRef{
					{Name: "researcher"}, // by name (description auto-discovered)
					coder.Ref(),          // by instance (carries its description)
				},
				MaxDelegations:   5,                                   // guard rail against runaway loops
				HistoryLength:    4,                                   // forward the last N user/model messages as context
				ArtifactStrategy: middlewarex.ArtifactStrategySession, // see "Sharing artifacts" below
			},
			&middlewarex.Artifacts{Readonly: true}, // read sub-agent artifacts via read_artifact
			&middleware.Retry{},
		),
	},
	aix.WithSessionStore(localstore.NewInMemorySessionStore[any]()),
)

Run it like any other agent:

out, _ := orchestrator.RunText(ctx,
	"Research the best sorting algorithms, then write a Go quicksort.")
fmt.Println(out.Message.Text())

middlewarex.Agents fields

  • Agents (required): sub-agent references ([]aix.AgentRef). Each is a bare {Name: ...} (description auto-discovered) or agent.Ref() (explicit).
  • ToolPrefix (*string): prefix for generated delegation tool names. nil defaults to "delegate_to" (tools become delegate_to_<agent>); a pointer to the empty string uses bare agent names.
  • MaxDelegations (int): cap on delegations per generate call. 0 means unlimited. Prevents runaway loops.
  • HistoryLength (int): number of recent user/model messages forwarded to a sub-agent as context. 0 sends only the task description. History is forwarded only to client-managed sub-agents (no session store); server-managed sub-agents receive only the task.
  • ArtifactStrategy (middlewarex.ArtifactStrategy): ArtifactStrategyInline (default) or ArtifactStrategySession — see below. The type and its constants live in plugins/middleware/exp alongside the Agents middleware.
  • Async (bool): enables background delegation (see below).
  • MaxWaitSeconds (int): caps one wait_for_background_tasks call, whatever timeout the model asks for. 0 lets the model decide.

Background delegation (Async: true)

Requires Genkit Go v1.14.0+.

By default a delegation blocks the orchestrator's turn. With Async: true, each delegation tool accepts background: true and returns a task ID immediately, and 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 session store (with detach support). Background work is tracked by a pending snapshot.

researcher := genkitx.DefineAgent(g, "researcher",
	aix.InlinePrompt{
		ai.WithModelName("googleai/gemini-flash-latest"),
		ai.WithSystem("Research one question and answer in a few sentences."),
	},
	aix.WithDescription[any]("Researches one question."),
	aix.WithSessionStore(localstore.NewInMemorySessionStore[any]()), // required for background
)

orchestrator := genkitx.DefineAgent(g, "orchestrator",
	aix.InlinePrompt{
		ai.WithModelName("googleai/gemini-flash-latest"),
		ai.WithSystem(`Give each part of a question to the researcher IN THE BACKGROUND
("background": true), then collect the answers with wait_for_background_tasks.`),
		ai.WithUse(&middlewarex.Agents{
			Agents: []aix.AgentRef{researcher.Ref()},
			Async:  true,
		}),
		ai.WithMaxTurns(10), // background work adds tool rounds
	},
	aix.WithSessionStore(localstore.NewInMemorySessionStore[any]()),
)

Continuing a task (continue_task)

Requires Genkit Go v1.14.0+.

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

  • failed / aborted: continues 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 from the parent snapshot.
  • interrupted: refused (no way to answer the interrupt from here).

Task IDs are scoped to the conversation: the tools accept only IDs this call or its history minted. Two Agents instances in one call need distinct ToolPrefix values.

Delegating to a remote agent

Requires Genkit Go v1.14.0+.

Any agent another Genkit app serves over HTTP (Go, JS, or Dart) can be a sub-agent. Register it with genkitx.DefineRemoteAgent; the orchestrator code is the same as for a local agent, background delegation included:

researcher := genkitx.DefineRemoteAgent(g, "researcher", "https://research.internal/agents/researcher",
	aix.WithDescription[any]("Researches one question and answers in a few sentences."),
	aix.WithAgentMetadata(&aix.AgentMetadata{
		StateManagement: aix.AgentStateManagementServer, // remote keeps its own session
		Abortable:       true,                            // its store can stop background work
	}),
	// aix.WithHTTPClient(idTokenClient), aix.WithHeaders(fn), aix.WithRouteURLs(...)
)

researcher.Ref() then goes in Agents as usual. The handle (*aix.AgentHandle) also runs the remote agent directly (Run, RunText, RunDetached, Task, WaitForSnapshot, Abort). The remote server needs a session store for background tasks and continue_task.

Sharing artifacts between agents

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

  • middlewarex.ArtifactStrategyInline (default): artifact content is included in the delegation tool result (so the model sees it directly) and merged into the parent session.
  • middlewarex.ArtifactStrategySession: artifacts are merged into the parent session only; the tool result lists artifact names, not content. Pair it with &middlewarex.Artifacts{} on the orchestrator so it can read_artifact on demand (the pattern above). Merged artifacts are namespaced by an invocation ID (<agentName>_<n>/<name>) and tagged with their source agent.

Combining with retries

plugins/middleware's Retry (and Fallback) attach the same way via ai.WithUse on an agent, and pair well with delegation:

ai.WithUse(&middleware.Retry{
	MaxRetries:     3,     // default 3
	InitialDelayMs: 1000,  // default 1000
	MaxDelayMs:     60000, // default 60000
	BackoffFactor:  2,     // default 2
})

See using middleware for the full catalog (Retry, Fallback, ToolApproval, Filesystem, Skills, SoftToolErrors, ContextCompression, Budget). A Budget on the orchestrator also counts in-process sub-agents, so delegation cannot spend around the cap.

Note: a sub-agent failure is reported back to the orchestrator as the delegation tool's output, with a task ID to continue_task it. A sub-agent interrupt is reported the same way but cannot be continued, 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.

1 warning2d3 checks · Risk SAFE
  • Gen Agent Trust Hub3d

    The skill provides comprehensive documentation and code samples for developing AI applications with Genkit in Go. It includes instructions for installing the Genkit CLI from an official source and documents security features like tool approval and filesystem sandboxing. No malicious patterns were detected.

  • Socket2d

    No alerts

  • Snyk3d

    Risk: MEDIUM · 2 issues

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 5 days ago
metadata
{
  "version": "1.0.0",
  "category": "AiAndMachineLearning"
}

README badge

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