Calling Another Agent (Rust)
Overview
The #[agent_definition] macro auto-generates a <AgentName>Client type for each agent, enabling agent-to-agent communication via RPC. An awaited call blocks the calling agent until the target agent returns a result.
Getting a Client
Use <AgentName>Client::upsert(...) with the target agent's constructor parameters:
let counter = CounterAgentClient::upsert("my-counter".to_string());
upsert connects to the keyed agent of these constructor parameters. If the agent does not exist yet, upsert creates it and returns after its initialization. If it already exists, you get a handle to the existing instance, and its constructor does not run again.
To connect to an existing agent by its agent ID (a UUID that Golem issues), use get_by_id. It never creates the agent and fails with GolemReflectError::RemoteNotFound when no agent of this type has the ID:
let id: golem_rust::AgentId = counter.agent_id().await?;
let same = CounterAgentClient::get_by_id(&id)?;
Awaited Call
Call a method and wait for the result:
let result = counter.increment().await;
let count = counter.get_count().await;
The calling agent blocks until the target agent processes the request and returns. This is the standard RPC pattern.
Multiple Agents With the Same Parameters
To create multiple distinct agents with the same constructor parameters, use the async create, which creates an agent without a key and with a new agent ID. See the golem-multi-instance-agent-rust skill.
Cross-Component RPC
When calling agents defined in a different component, the generated client type is available after running golem build — the build step generates bridge SDK code for inter-component dependencies declared in golem.yaml.
Avoiding Deadlocks
Never create RPC cycles where A awaits B and B awaits A — this deadlocks both agents. Use trigger_ (fire-and-forget) to break cycles. See the golem-fire-and-forget-rust skill.