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

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

Working with Agent State (Experimental)

Experimental / preview API. Read agents.md first.

Beyond message history, an agent session can hold typed custom state — your own structured data (a task list, a workflow status, counters, etc.). Tools read and mutate it during a turn, and every mutation is streamed to a connected client as a JSON Patch so the client's view stays live.


Declare the state shape

The custom-state type is the State type parameter of the agent (and its store). It lives under Custom of aix.SessionState[State].

type TaskItem struct {
	ID    int    `json:"id"`
	Title string `json:"title"`
	Done  bool   `json:"done"`
}

type TaskState struct {
	Tasks  []TaskItem `json:"tasks"`
	NextID int        `json:"nextId"`
}

taskAgent := genkitx.DefineAgent(g, "taskAgent",
	aix.InlinePrompt{
		ai.WithModelName("googleai/gemini-flash-latest"),
		ai.WithSystem("You manage the user's task list. Use the tools to modify it."),
		ai.WithTools(addTask), // defined below
	},
	aix.WithSessionStore(localstore.NewInMemorySessionStore[TaskState]()),
)

Read and mutate state inside tools

Inside a tool, get the live session from context with aix.SessionFromContext[State](ctx), then read with session.Custom() and mutate with session.UpdateCustom(fn). UpdateCustom takes func(State) State and writes the result back atomically. When the session is driven by an agent invocation, the mutation is automatically streamed to the client as an AgentStreamChunk.CustomPatch.

type AddTaskInput struct {
	Title string `json:"title"`
}

addTask := genkit.DefineTool(g, "addTask",
	"Add a new task. Returns the created task.",
	func(ctx *ai.ToolContext, in AddTaskInput) (TaskItem, error) {
		session := aix.SessionFromContext[TaskState](ctx.Context)
		if session == nil {
			return TaskItem{}, fmt.Errorf("addTask must run inside an agent session")
		}
		var created TaskItem
		session.UpdateCustom(func(s TaskState) TaskState {
			if s.NextID == 0 {
				s.NextID = 1
			}
			created = TaskItem{ID: s.NextID, Title: in.Title, Done: false}
			s.Tasks = append(s.Tasks, created)
			s.NextID++
			return s
		})
		return created, nil
	},
)

aix.SessionFromContext[State] returns nil outside an active session (or when the State type does not match), so guard against it. Note the tool's context is ctx.Context (the *ai.ToolContext embeds the standard context.Context).

Do not call other Session methods (or send on a Responder) from inside the UpdateCustom callback: it runs while the session lock is held and would deadlock.

Seed and read state

Seed initial custom state with aix.WithState on the first turn. For a server-managed agent, later turns resume with aix.WithSessionID and the state is loaded from the snapshot.

out, _ := taskAgent.Run(ctx,
	&aix.AgentInput{Message: ai.NewUserTextMessage("Add a task: buy groceries")},
	aix.WithState(&aix.SessionState[TaskState]{
		Custom: TaskState{Tasks: nil, NextID: 1},
	}),
)
// For a client-managed agent, the final state is on out.State:
if out.State != nil {
	fmt.Printf("%+v\n", out.State.Custom)
}

Live state on a connection

A connection tracks custom state as it streams: each Receive()d chunk carries a CustomPatch (an RFC 6902 JSON Patch delta), which the connection applies to an internal copy. Read it any time with conn.Custom().

conn, _ := taskAgent.Connect(ctx,
	aix.WithState(&aix.SessionState[TaskState]{Custom: TaskState{NextID: 1}}),
)
_ = conn.SendText("Add buy groceries, then mark it done")
for chunk, err := range conn.Receive() {
	if err != nil {
		log.Fatal(err)
	}
	if len(chunk.CustomPatch) > 0 {
		cur, _ := conn.Custom() // TaskState reflecting deltas so far
		fmt.Printf("live: %+v\n", cur)
	}
	if chunk.TurnEnd != nil {
		break
	}
}

The first CustomPatch of each turn is a whole-document replace (re-basing the client); later patches are incremental diffs. The authoritative final state is on out.State (client-managed) or the turn-end snapshot (server-managed).

Prompt templating with state

Inside a prompt template, the session's custom state is available as {{@state}}, evaluated fresh at render time so the template always sees the latest values.

ai.WithSystem("The user's current task list: {{json @state.tasks}}. Help them manage it."),

Custom status streaming from a custom agent

For live mid-turn progress (a status field that updates as a long turn runs), mutate custom state from a custom agent: every UpdateCustom emits a CustomPatch chunk automatically, so the connection's Custom() stays live without extra wiring.

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