Skip to Content
DevelopAdditional runtime APIs

Additional Runtime APIs

Generate an idempotency key

Golem provides a function to generate an idempotency key (a UUID) which can be passed to external systems to ensure that the same request is not processed multiple times.

It is guaranteed that this idempotency key will always be the same (per occurrence) even if the agent is restarted due to a crash.

To generate an idempotency key:

import { generateIdempotencyKey } from "@golemcloud/golem-ts-sdk"; const key = generateIdempotencyKey();

Get agent metadata

It is possible to query metadata for Golem agents. This metadata is defined by the AgentMetadata interface:

/** * Metadata about an agent */ export type AgentMetadata = { /** The agent ID, consists of the component ID, agent type and agent parameters */ agentId: AgentId; /** Command line arguments seen by the agent */ args: string[]; /** Environment variables seen by the agent */ env: [string, string][]; /** Configuration variables seen by the agent */ config: [string, string][]; /** The current agent status */ status: AgentStatus; /** The component revision the agent is running with */ componentRevision: bigint; /** The agent's current retry count */ retryCount: bigint; /** The environment the agent belongs to */ environmentId: EnvironmentId; };

There are two exported functions to query agent metadata:

  • getSelfMetadata() returns the metadata for the current agent
  • getAgentMetadata(agentId: AgentId) returns the metadata for a specific agent given by its AgentId, or undefined if it does not exist

Enumerate agents

Agent enumeration is a feature of Golem available both through the public HTTP API and using the SDK.

Enumerating agents of a component is a slow operation and should not be used as part of the application logic.

The following example demonstrates how to use the agent enumeration API:

import { ComponentId, GetAgents, AgentAnyFilter, AgentMetadata, AgentStatusFilter, } from "@golemcloud/golem-ts-sdk" const filter: AgentAnyFilter = { filters: [ { filters: [ { tag: "status", val: { comparator: "equal", value: "idle", } satisfies AgentStatusFilter, }, ], }, ], } declare const componentId: ComponentId const agents: AgentMetadata[] = [] const getter = new GetAgents(componentId, filter, true) let batch: AgentMetadata[] | undefined while ((batch = getter.getNext()) !== undefined) { agents.push(...batch) }

The precise parameter or option enables precise mode. In this mode, Golem calculates the latest metadata for each returned agent; otherwise, it uses only the last cached values.

Update an agent

To trigger an update for a given agent from one component version to another, use the updateAgent function:

import { getSelfMetadata, updateAgent, type AgentId, type ComponentRevision, } from "@golemcloud/golem-ts-sdk" const agentId: AgentId = getSelfMetadata().agentId const targetRevision: ComponentRevision = 1n updateAgent(agentId, targetRevision, "automatic")

To learn more about updating agents, see the Agent Update section of the agents page.

Oplog search and query

Golem provides APIs to read and search an agent’s persisted oplog.

Each oplog entry describes a recorded operation or lifecycle event. You can either read all entries or supply a search expression to return only matching entries.

Results are paginated. Read successive batches until the query returns no more entries. For CLI usage, see Observability.

Invocation context

Golem associates an invocation context with each invocation, which contains various information depending on how the exported function was called. This context gets inherited when making further invocations via agent-to-agent communication, and it is also possible to define custom spans and associate custom attributes to it.

The spans are not automatically sent to any tracing system but they can be reconstructed from the oplog, for example using oplog processor plugins, to provide real-time tracing information.

The TypeScript SDK does not currently expose invocation-context inspection or custom span creation through its public API. See Observability for logs and trace export.

The custom spans are pushed onto the invocation context stack, so whenever an RPC call or HTTP call is made, their parent span(s) will include the user-defined custom spans as well as the rest of the invocation context.

To export traces, logs, and metrics to an external system, see the Observability page for setting up the OpenTelemetry plugin.

Key-value store

Although Golem agents can store their state completely in their own memory, they can also store key-value pairs in Golem-managed storage.

This can be useful if state needs to be shared between different agents or if the size of this state is too large to be stored in memory. Storage is shared across agents and components within the same environment.

Use a small value for shared settings. A missing key is distinct from a storage failure; do not turn every failure into a default value. The examples below write and read one greeting. Separate calls do not form a transaction.

Use openBucket to open a bucket. Opening is asynchronous; the bucket’s methods are synchronous and work with Uint8Array values. A missing key returns undefined; storage failures throw.

import { openBucket } from "@golemcloud/golem-ts-sdk" async function greeting(): Promise<Uint8Array | undefined> { const bucket = await openBucket("shared") bucket.set("greeting", new TextEncoder().encode("hello")) return bucket.get("greeting") }
MethodResult
get(key)The stored bytes, or undefined when the key does not exist
set(key, value)Creates or replaces a value
delete(key)Deletes a key, if present
exists(key)Whether a key exists
getMany(keys)One value or undefined for each key
setMany(entries)Stores several key-value pairs
deleteMany(keys)Deletes several keys
keys()All keys in the bucket

bucket.forSchema(schema) provides the same operations with schema-validated values instead of bytes.

Blob store

The blob store provides a way to store and retrieve large binary data. This can be useful for storing large files or other binary data that is too large to be stored in the agent’s memory. Storage is shared across agents and components within the same environment.

Read a bounded range instead of loading a large object into memory. Each example below opens the existing documents container and reads the first five bytes of hello.txt, which must contain at least five bytes. Start and end offsets are inclusive. Releasing a handle does not delete the stored data.

Use getContainer to open an existing container or getOrCreateContainer to open or create one. Container operations return promises with Uint8Array values; storage failures reject the promise.

import { getContainer } from "@golemcloud/golem-ts-sdk" async function documentPrefix(): Promise<Uint8Array> { const container = await getContainer("documents") return await container.getData("hello.txt", 0n, 4n) }
MethodResult
getData(name, start?, end?)Object bytes as a Uint8Array; optional offsets are inclusive
writeData(name, bytes)Creates or replaces an object from a Uint8Array
listObjects()An array of object names
has(name)Whether an object exists
delete(name)Deletes an object, if present
deleteMany(names)Deletes several objects
objectInfo(name)Object metadata, including its size
info()Container metadata
clear()Deletes all objects in the container

The SDK also exports containerExists, deleteContainer, copyObject, and moveObject. Copy and move locations have the shape { container: string, object: string }. Use container.forSchema(schema) for schema-validated object values instead of bytes.