Golem multi instance agent go

From golemcloud/golem · View interactive version

Golem multi instance agent go is an AI agent skill from the GitHub repository golemcloud/golem. Addressing multiple instances of a Go agent by key or agent ID. Use when the user wants many instances of an agent (one per user/tenant/session), a composite key, agents without a key, or ephemeral instances in a Go Golem project.

How to use this skill

Skill file

Multiple Instances of a Go Agent

Overview

An agent type has many instances. Each instance has an agent ID, a golem.AgentID (a UUID) that Golem issues when it creates the agent. An instance can also have a key: its constructor parameters, the ID struct. Agent.Upsert(id) returns a client for the keyed agent of that ID — two Upserts with the same id address the same agent, created implicitly on first use. Agent.Create(id) creates a new agent with a new agent ID and no key; such an agent is reachable only by its agent ID, with Agent.GetByID(agentID). Neither changes Spec.Mode, which defaults to durable.

Key: single or composite

The ID struct's fields are the key — use several fields for a composite key:

package session

import "github.com/golemcloud/golem/sdks/go/golem"

// One session instance per (user, device).
type ID struct {
	User   string
	Device string
}

var Agent = golem.DefineAgent[ID](golem.Spec{Name: "SessionAgent", Description: "Per-user, per-device session"})

var Touch = Agent.Method[golem.Unit, int64]("touch", golem.Description("Record activity, return hit count"))

Addressing a specific instance

// Keyed instance for this (user, device) — the same id always maps to the same agent.
c := session.Agent.Upsert(session.ID{User: "alice", Device: "laptop"})
hits := session.Touch.MustCall(c, golem.Unit{})

Iterate ids to address many instances:

for _, u := range users {
    c := session.Agent.Upsert(session.ID{User: u, Device: "web"})
    session.Touch.MustTrigger(c, golem.Unit{}) // fan out
}

Agents without a key

Create(id, opts...) creates a new agent with a new agent ID and no key each time it is called, and returns a client once its constructor has finished. The constructor still receives id. Save the agent ID (Client.AgentID()) to reach the same agent again later with GetByID:

c, err := session.Agent.Create(session.ID{User: "alice", Device: "kiosk"})
if err != nil {
	return err
}
session.Touch.MustCall(c, golem.Unit{})

// To reach the SAME agent again later, save and reuse its agent ID:
agentID := c.MustAgentID()
c2, err := session.Agent.GetByID(agentID) // never creates; an unknown ID is golem.RemoteNotFound
if err != nil {
	return err
}
_ = c2

Create takes golem.CreateOpts: golem.Keyed() creates the keyed agent of id instead (the one Upsert reaches) and fails with a *golem.RemoteCallError of kind golem.RemoteConflict when it exists; golem.WithEnv(name, value) adds an environment variable; Agent.WithConfig(cfg) overrides configuration (see golem-add-config-go). MustCreate and MustGetByID panic instead of returning the error. Agent.LookupID(id) returns the agent ID of the keyed agent of id, or None when it does not exist, without creating it.

Ephemeral mode

Ephemeral types set Mode: golem.Ephemeral. They are addressed with Upsert only: each invocation through the client runs on a new one-shot instance, so a client can be reused for independent invocations. Create and GetByID refuse an ephemeral type.

Key Constraints

Related Skills

| Skill | When to Load |

|-------|--------------|

| golem-add-agent-go | Define the agent type and its ID |

| golem-call-another-agent-go | Call a specific instance from another agent |

| golem-add-http-endpoint-go | Select an instance from a request URL |

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.