Golem multi instance agent effect

From golemcloud/golem · View interactive version

Golem multi instance agent effect is an AI agent skill from the GitHub repository golemcloud/golem. Creating and reconnecting multiple Effect-based agent instances with identical agent id fields. Use for agents without a key, client.create/client.getById, agent IDs, or independent per-instance state with @golemcloud/effect-golem.

How to use this skill

Skill file

Multiple Agents with Identical Agent Id Fields in Effect Golem

Every agent has an agent ID: a UUID that Golem issues. The Type(params) text of the agent type

and its agent id fields is the agent's key. Repeated client.upsert calls with the same agent id

record reach the same keyed agent. Each durable client.create call without keyed creates a new

agent without a key: it has a new agent ID and its own state, and only that ID reaches it again.

Collector("shared")                                   key: the keyed agent
Collector[a09f61a8-677a-80ea-9ebe-437a0df51749]       ID
a09f61a8-677a-80ea-9ebe-437a0df51749                  ID

The keyed agent and the agents without a key above never share state.

Typed Client API

Every value returned by defineAgent(...) exposes a typed client. Effect clients use named

agent id records and return Effect values. This table describes durable targets:

| API | Meaning |

| --- | --- |

| Agent.client.upsert({ name: "shared" }) | Address the keyed agent, and create it when it is missing |

| Agent.client.create({ name: "shared" }) | Create a new agent without a key, with a new agent ID |

| Agent.client.create({ name: "shared" }, { keyed: true }) | Create the keyed agent; fails with an RpcCallError whose cause is conflict when it exists |

| Agent.client.getById(agentId) | Reconnect to the existing agent with that typed AgentId; an unknown ID fails with a not-found cause |

| remote.agentId | Effect that reads the typed AgentId of the connected agent |

upsert and getById accept { scopeCard } as their last argument; create accepts

{ keyed, config, env, scopeCard }. Do not use positional agent id fields, put the ID in the

agent id record, or translate static methods from the Promise-based TypeScript SDK.

Remote methods also take their declared named parameter record. Pass {} to a no-parameter

method. Yield both the client constructor Effect and each remote method Effect:

const collector = yield* Collector.client.create({ name });
yield* collector.addValue({ value: 10 });
const total = yield* collector.getTotal({});

Client construction and remote calls have typed RPC/configuration errors. Handle or map them when

they are domain-relevant. Use Effect.orDie to turn an unexpected failure into a defect rather

than declaring an unrelated domain error. An unhandled defect fails the invocation without retry.

Host retry policies govern retryable RPC failures. Use an SDK saga for business-level

compensation and explicit guest-code attempts.

Stateful Multi-Instance Example

Define the target as a normal durable agent. Whether an agent has a key changes how it is

addressed; it does not require a different agent definition.

import { Effect, Ref, Schema } from "effect";
import { defineAgent, method, Snapshot } from "@golemcloud/effect-golem";

const Values = Schema.Array(Schema.Number);

export const Collector = defineAgent({
  name: "Collector",
  mode: "durable",
  id: {
    name: Schema.String,
  },
  snapshotting: Snapshot.define({
    schema: Values,
    policy: Snapshot.policy.everyN(10),
  }),
  methods: {
    addValue: method({
      input: { value: Schema.Number },
      success: Schema.Void,
    }),
    getValues: method({
      input: {},
      success: Values,
    }),
    getTotal: method({
      input: {},
      success: Schema.Number,
    }),
  },
}).implement({
  init: () => Ref.make<ReadonlyArray<number>>([]),
  methods: (values) => ({
      addValue: ({ value }) =>
        Ref.update(values, (current) => [...current, value]),
      getValues: () => Ref.get(values),
      getTotal: () =>
        Ref.get(values).pipe(
          Effect.map((current) =>
            current.reduce((total, value) => total + value, 0),
          ),
        ),
  }),
  snapshot: Snapshot.ref<ReadonlyArray<number>>(),
});

export const CollectorCoordinator = defineAgent({
  name: "CollectorCoordinator",
  mode: "durable",
  id: {
    name: Schema.String,
  },
  methods: {
    spawnAndFillCollector: method({
      input: {},
      success: Schema.Number,
    }),
    spawnTwoCollectors: method({
      input: {},
      success: Schema.Array(Schema.Number),
    }),
    readKeyedCollector: method({
      input: {},
      success: Schema.Number,
    }),
  },
}).implement({
  init: ({ name }) => Effect.succeed(name),
  methods: (name) => ({
    spawnAndFillCollector: () =>
      Effect.gen(function* () {
        const collector = yield* Collector.client.create({ name });
        yield* collector.addValue({ value: 10 });
        yield* collector.addValue({ value: 20 });
        return yield* collector.getTotal({});
      }).pipe(Effect.scoped, Effect.orDie),

    spawnTwoCollectors: () =>
      Effect.gen(function* () {
        const first = yield* Collector.client.create({ name });
        const second = yield* Collector.client.create({ name });

        yield* first.addValue({ value: 1 });
        yield* second.addValue({ value: 2 });

        const firstTotal = yield* first.getTotal({});
        const secondTotal = yield* second.getTotal({});
        return [firstTotal, secondTotal];
      }).pipe(Effect.scoped, Effect.orDie),

    readKeyedCollector: () =>
      Effect.gen(function* () {
        const collector = yield* Collector.client.upsert({ name });
        return yield* collector.getTotal({});
      }).pipe(Effect.scoped, Effect.orDie),
  }),
});

In an existing application, merge the coordinator methods and handlers into the existing agent

instead of registering a duplicate agent type. Import every implementation module from

src/main.ts using its emitted .js suffix.

Save and Reconnect by Agent ID

Read the typed AgentId of a created agent with remote.agentId. Persist it in

schema-serializable durable state with the Ids.AgentId schema, or as text (an AgentId is a

branded string), if another invocation must reconnect later:

import { AgentId } from "@golemcloud/effect-golem";

const first = yield* Collector.client.create({ name: "shared" });
const firstId = yield* first.agentId;
const savedText: string = firstId;

const sameInstance = yield* Collector.client.getById(yield* AgentId.parse(savedText));
const values = yield* sameInstance.getValues({});

getById never creates an agent: an unknown ID, or the ID of an agent of another type, fails with

an RpcCallError whose cause is not-found. AgentId.parse checks only the UUID text form and

fails with AgentIdParseError. Inside the implementation, read the agent's own ID:

import { SelfAgentId } from "@golemcloud/effect-golem";

const ownId = Effect.gen(function* () {
  return yield* SelfAgentId.SelfAgentId; // AgentId
});

Golem also sets GOLEM_AGENT_ID, GOLEM_AGENT_TYPE, and, only for a keyed agent,

GOLEM_AGENT_KEY in the agent's environment. Self metadata reads require view permission on the

running agent. Agents.getSelfMetadata exposes the same ID plus full metadata, including stored

environment and config fields. Self host failures through SelfAgentId are defects. Read again

after a fork rather than relying on a previously saved ID.

Ephemeral and HTTP Cases

instance and its state is not durable. An EphemeralRemoteAgent has no agentId; its awaited

method calls return { value, metadata }, and metadata.agentId is the ID of the one-shot

instance. Do not use the durable save-and-reconnect example with an ephemeral handle.

const ephemeralCall = Effect.gen(function* () {
  const remote = yield* EphemeralWorker.client.upsert({ name: "task" });
  const { value, metadata } = yield* remote.run({});
  return { value, metadata };
});
import { Http } from "@golemcloud/effect-golem";

http: Http.mount("/api/{name}", { phantomAgent: true }),

This HTTP option asks the host for a fresh instance per request. The handler reads the instance's

ID with SelfAgentId.SelfAgentId.

Key Constraints

from remote.agentId, SelfAgentId.SelfAgentId, or the CLI; never build one yourself.

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.