Golem agent reflection go

From golemcloud/golem · View interactive version

Golem agent reflection go is an AI agent skill from the GitHub repository golemcloud/golem. Calling Golem agents from Go without sharing their Go definition — caller-defined method-only and full agent clients, discovered (reflected) clients with canonical JSON, and fully dynamic invocation with packed values. Use when agent types or methods are selected at runtime, many agent types share a

How to use this skill

Skill file

Calling Agents with Runtime Reflection (Go)

Normal RPC through the callee's definition package or a generated guest client (see golem-call-another-agent-go) is the baseline. Reflection adds three more levels, which compose through agent IDs:

| Level | Use it when | Go |

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

| Caller-defined, method-only | Many agent types share a method shape; you have an agent ID | golem.DefineAgentClient[Shape]() + Method[In, Out], GetByID(id) |

| Caller-defined, full | You own Go types for one target type, without its definition | golem.DefineFullAgentClient[Id](name, spec) + Method[In, Out] |

| Discovered | The type or method is chosen at runtime and should be validated | reflection.DiscoverAgentType → Upsert / Create / GetByID → Call |

| Fully dynamic | Values are already packed; no schema authority kept | reflection.GetByID(id) → Call |

Every level uses the same verbs: Call waits for the result, Trigger does not, Schedule runs it later. Discovered and dynamic clients live in the reflection package (github.com/golemcloud/golem/sdks/go/golem/reflection).

Method-Only Clients

A method-only client declares methods and nothing else, so it calls any existing agent that has them, whatever its type:

type Pingable struct{} // the shape's identity type

var (
	Pinger = golem.DefineAgentClient[Pingable]()
	Ping   = Pinger.Method[golem.Unit, string]("ping")
)

func pingAll(ids []golem.AgentID) {
	for _, id := range ids {
		c, err := Pinger.GetByID(id)
		if err != nil {
			continue
		}
		reply, err := Ping.Call(c, golem.Unit{})
		fmt.Println(id, reply, err)
	}
}

GetByID addresses the existing agent with the ID, of the type the ID names, and checks nothing else about the target, which the client does not describe; an unknown ID is a *golem.RemoteCallError of kind golem.RemoteNotFound, and a method mismatch is reported by the host or when the result is decoded. It never creates the agent.

Full Clients

A full client also declares the target type's name, constructor and lifecycle, so it creates and addresses instances like the agent's own definition:

type CounterID struct{ Name string }
type AddIn struct{ By int64 }

var (
	Counter = golem.DefineFullAgentClient[CounterID]("CounterAgent", golem.AgentClientSpec{})
	Add     = Counter.Method[AddIn, int64]("add")
)

c, err := Counter.Upsert(CounterID{Name: "main"}) // the keyed agent; an error when the target is not deployed
total, err := Add.Call(c, AddIn{By: 5})

fresh, err := Counter.Create(CounterID{Name: "main"}) // a new agent with a new agent ID and no key
agentID, err := fresh.AgentID()
c, err = Counter.GetByID(agentID) // the existing agent; RemoteNotFound for an unknown ID or another type
found, err := Counter.LookupID(CounterID{Name: "main"}) // golem.Option[golem.AgentID] of the keyed agent, without creating it

Generated guest clients are built on full clients.

Discover and Inspect

for _, t := range reflection.DiscoverAgentTypes() {
	fmt.Println(t.Name(), t.Mode(), t.SourceLanguage())
	for _, m := range t.Methods() {
		fmt.Println("  ", m.Name(), m.Description(), m.ReadOnly())
	}
}

counter, found := reflection.DiscoverAgentType("CounterAgent")
if !found {
	panic("CounterAgent is not deployed in this environment")
}
add, known := counter.Method("add")
if !known {
	panic("CounterAgent has no method add")
}
input := add.Input()                              // a schema.Ref of the parameter record
fmt.Println(golem.Must(input.ToJSONSchema(true))) // e.g. for an LLM
if output, hasOutput := add.Output().Get(); hasOutput {
	fmt.Println(golem.Must(output.ToJSONSchema(true)))
}

Input() returns a schema.Ref for the parameter record (without host-filled fields such as the principal). Output() returns a golem.Option[schema.Ref]: golem.None[schema.Ref]() when there is no result. Use Get() and check its boolean before accessing the result schema. A schema.Ref from github.com/golemcloud/golem/sdks/go/core/schema packs and unpacks canonical JSON and renders JSON Schema. Constructor().Input() describes the constructor the same way. A discovery miss (not deployed, not visible to the caller) reports false. A snapshot never refreshes itself; discover again when a newer deployment matters.

Call a Discovered Agent

client, err := counter.Upsert(map[string]any{"name": "main"})
if err != nil {
	panic(err)
}
total, invocation, err := client.Call("add", map[string]any{"by": "5"}) // s64 is a string
if err != nil {
	var rce *golem.RemoteCallError
	if errors.As(err, &rce) {
		fmt.Println(rce.Kind, rce.Message)
	}
	panic(err)
}
fmt.Println(golem.Must(client.AgentID()), total, invocation.InvocationID)

Agent IDs

A golem.AgentID is an opaque UUID that Golem issues; its text form (String(), and JSON) is the UUID. The agent's constructor parameters cannot be read out of it: address an agent by its constructor parameters with Upsert, and by its agent ID with GetByID.

agentID := golem.AgentIDFromUUID(golem.Must(golem.ParseUUID(text))) // or json.Unmarshal into a golem.AgentID

t, found := reflection.DiscoverAgentTypeByID(agentID) // the type behind an existing agent
client := golem.Must(t.GetByID(agentID))
other := golem.Must(t.LookupID(map[string]any{"name": "other"})) // golem.Option[golem.AgentID]

Lookup by ID never creates the agent. Agent IDs are scoped to the caller's environment.

Fully Dynamic

A dynamic client sends values the caller packed itself and validates nothing. Keep a discovered snapshot beside it for validation:

t, _ := reflection.DiscoverAgentType(agentType)
m, _ := t.Method(method)
input := golem.Must(m.Input().PackJSON(args))

client := golem.Must(reflection.GetByID(agentID))
out, _, err := client.Call(method, input)
if err != nil {
	panic(err)
}
if v, has := out.Get(); has {
	if result, declared := m.Output().Get(); declared {
		fmt.Println(golem.Must(result.UnpackJSON(v)))
	}
}

A dynamic client also has CallAsync, Trigger and Schedule. reflection.GetByID reaches an existing agent only; to create one or reach a keyed agent by its constructor parameters, use a discovered client's Upsert or Create.

Canonical JSON

Related Skills

| Skill | When to Load |

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

| golem-agent-reflection | Choosing between client approaches, identity rules |

| golem-call-another-agent-go | Typed RPC through definitions and guest clients |

| golem-call-tool-go | Calling tools the same ways |

| golem-multi-instance-agent-go | Keys, agent IDs and agents without a key |

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.