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
- The
IDfields are the key: the same key addresses the same keyed agent. An agent created withCreate(withoutgolem.Keyed()) has no key and is reachable only by its agent ID. - The agent ID is an opaque UUID: the agent type and constructor parameters cannot be read out of it.
- Instances are durable by default. Set
Spec.Modeseparately for ephemeral execution; do not treat an ephemeral invocation's agent ID as a reusable identity. - Over HTTP, each
IDfield must appear as a{var}in the mount path so a URL selects the instance (seegolem-add-http-endpoint-go).
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 |