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-background.md

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

Background Agents / Detaching (Experimental)

Experimental / preview API. Detaching requires a session store that also implements aix.SnapshotSubscriber (both localstore stores do) — the server needs somewhere to write the result and a way to observe an abort. Read agents.md first.

Detaching runs a turn in the background: the server writes a pending snapshot and returns its SnapshotID immediately, keeps processing, then rewrites the snapshot to a terminal status (completed / failed / aborted). A reader waits on the snapshot (or polls it) and can Abort in the meantime.


Define the agent (store required)

backgroundAgent := genkitx.DefineAgent(g, "backgroundAgent",
	aix.InlinePrompt{
		ai.WithModelName("googleai/gemini-flash-latest"),
		ai.WithSystem("You are a senior research analyst. Produce a comprehensive markdown report."),
	},
	aix.WithSessionStore(localstore.NewInMemorySessionStore[any]()), // REQUIRED for detach
)

Detach a turn

RunDetached sends one input, returns once the server has accepted the work, and hands back a *aix.DetachedTask[State]. Store its snapshot ID; agent.Task(snapshotID) rebuilds the same handle in any process later.

task, err := backgroundAgent.RunDetached(ctx, &aix.AgentInput{
	Message: ai.NewUserTextMessage("Write a report on renewable energy trends"),
})
if err != nil {
	log.Fatal(err) // the work never started
}
snapshotID := task.SnapshotID() // pending; save this

On a live connection, send an input with Detach: true (or call conn.Detach() mid-turn); the output has FinishReason == aix.AgentFinishReasonDetached and the pending SnapshotID.

Wait (or poll)

waitCtx, cancel := context.WithTimeout(ctx, 5*time.Minute)
defer cancel()

snap, err := task.Wait(waitCtx) // blocks until settled; no polling loop needed
if err != nil {
	log.Fatal(err) // the wait itself failed (unknown snapshot, ctx done)
}
switch snap.Status {
case aix.SnapshotStatusCompleted:
	msgs := snap.State.Messages
	fmt.Println(msgs[len(msgs)-1].Text())
case aix.SnapshotStatusFailed, aix.SnapshotStatusAborted:
	// Resume points: State holds the turns that finished (see below).
case aix.SnapshotStatusExpired:
	// The worker died (heartbeat stale). Resume from snap.ParentID and resend.
}

task.Poll(ctx) does one read instead. agent.WaitForSnapshot(ctx, id) waits on a bare snapshot ID. Pass aix.WithMetadataOnly() to Poll / GetSnapshot to read status without loading State. Wait returns the snapshot for every outcome; a non-nil error means the wait could not proceed.

Abort and resume

status, err := task.Abort(ctx) // or backgroundAgent.Abort(ctx, snapshotID)

An abort flips pending to aborting (so Abort answers aborting for a run still going), the worker winds down, then the row settles. Check what Wait returns rather than assuming aborted: a turn that finished anyway settles completed, and one that broke first settles failed.

failed and aborted snapshots are resume points: they hold the conversation up to the last completed turn. Send an input with no payload to re-run the turn, or a new message to change course:

snap, _ := task.Wait(ctx)
if snap.Status == aix.SnapshotStatusAborted || snap.Status == aix.SnapshotStatusFailed {
	out, err := backgroundAgent.Run(ctx, &aix.AgentInput{},
		aix.WithSnapshotID[any](snap.SnapshotID))
	// ...
}

Detached work runs in the process that started it; it is not durable execution. On SIGTERM, abort pending snapshots and wait for them to settle so callers find resumable aborted rows instead of expired ones.

Serve detach over HTTP

A remote client drives the same story over HTTP: send {"detach": true, ...} as the turn input, then wait on the waitForSnapshot companion (or poll getSnapshot) and hit abort. Expose the companions next to the agent:

mux.HandleFunc("POST /api/backgroundAgent", genkit.Handler(backgroundAgent))
mux.HandleFunc("POST /api/backgroundAgent/getSnapshot", genkit.Handler(backgroundAgent.GetSnapshotAction()))
mux.HandleFunc("POST /api/backgroundAgent/waitForSnapshot", genkit.Handler(backgroundAgent.WaitForSnapshotAction()))
mux.HandleFunc("POST /api/backgroundAgent/abort", genkit.Handler(backgroundAgent.AbortAction()))

One waitForSnapshot request holds for at most aix.WithMaxSnapshotWait (default 25s), then answers with the row as it stands (pending/aborting); a direct HTTP client must loop until settled. aix.AgentHandle (local or genkitx.DefineRemoteAgent) does that for you. AbortAction() is non-nil only when the store implements aix.SnapshotSubscriber. See deployment.

Status values (aix.SnapshotStatus)

  • SnapshotStatusPending: still processing.
  • SnapshotStatusAborting: abort requested; the worker is winding down. Not terminal: Wait keeps waiting.
  • SnapshotStatusCompleted: finished successfully; read the result from snap.State.Messages.
  • SnapshotStatusFailed: a turn broke; details on snap.Error, completed turns in snap.State. Resumable.
  • SnapshotStatusAborted: stopped by the caller; snap.State holds the turns that finished. Resumable.
  • SnapshotStatusExpired: the background worker stopped responding (its heartbeat went stale, e.g. a server restart). Computed on read, never persisted; terminal — the task can never complete.

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