# Golem Call Another Agent TS

> Calling another agent and awaiting the result in a TypeScript Golem project. Use when the user asks about agent-to-agent RPC, calling remote agents, or inter-component communication.

- Skill: `golemcloud/golem-call-another-agent-ts` (Agent Skill)
- Install (CLI): `npx skillmds@latest add golemcloud/golem-call-another-agent-ts`
- Raw SKILL.md: https://api.skillmd.com/api/skills/golemcloud/golem-call-another-agent-ts/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Web & Frontend
- Author: golemcloud (https://skillmd.com/u/golemcloud)
- Updated: 2026-09-17
- Page: https://skillmd.com/skills/golemcloud/golem-call-another-agent-ts

---


# Calling Another Agent (TypeScript)

There are two typed RPC APIs. Use a definition's `.client` when both agents are defined in the
same component. Use a generated guest client when the target is in another
component (including a component written in another language).

## Same Component: Definition Client

Use the `.client` attached to the agent's **definition** (the value returned by
`defineAgent`), then call `.get` with the target agent's **id record**:

```typescript
import { Counter } from './counter-agent.js';

// Get a handle to a specific instance by its id record.
const c1 = Counter.client.get({ name: 'my-counter' });
```

The id argument is the record declared in the target agent's `id: { … }` (for a
`Counter` with `id: { name: z.string() }`, that is `{ name: 'my-counter' }`).
This does **not** create the agent — the agent is created implicitly on its first
invocation. If it already exists, you get a handle to the existing instance.

Call a method and wait for the result. Method inputs are passed as the declared
input record (methods with an empty `input: {}` take no argument):

```typescript
const count = await c1.increment();          // input: {}
const next = await c1.add({ by: 5 });        // input: { by: z.number() }
```

The calling agent **blocks** until the target agent processes the request and
returns. This is the standard RPC pattern. On failure (or an error result) the
call throws a `RemoteCallError`.

## Phantom Agents

Call `Def.client.getPhantom(id, phantomId)` to address a specific phantom
instance that shares the same id record. To create a fresh phantom, call
`Def.client.newPhantom(id)`; the returned details contain the typed client, full
`agentId`, and generated `phantomId`, which can be saved and reused. See the
`golem-multi-instance-agent-ts` skill.

## Different Component: Generated Guest Client

Declare the dependency in the application-level `golem.yaml`. The CLI infers the
consumer language from its `ts` template and generates the client before
compiling the dependent component:

```yaml
components:
  my-app:counter:
    dir: counter
    templates: rust
  my-app:consumer:
    dir: consumer
    templates: ts
    dependencies:
      agents:
        - my-app:counter/CounterAgent
```

The generated package is
`golem-temp/bridge-sdk/ts/internal/counter-agent-guest-client`. From the
`consumer` component directory, wire its source into the existing
`tsconfig.json` (keep the component's own `src/**/*.ts` include):

```json
{
  "compilerOptions": {
    "paths": {
      "counter-agent-guest-client": [
        "../golem-temp/bridge-sdk/ts/internal/counter-agent-guest-client/counter-agent-guest-client.ts"
      ]
    }
  },
  "include": [
    "src/**/*.ts",
    "../golem-temp/bridge-sdk/ts/internal/counter-agent-guest-client/*.ts"
  ]
}
```

Import that alias. Generated constructors flatten the target's id fields into
parameters rather than taking an id record:

```typescript
import { CounterAgent } from 'counter-agent-guest-client';

const counter = CounterAgent.get('my-counter');
const count = await counter.increment();

counter.increment.trigger();
counter.increment.schedule(runAt);
const cancellation = counter.increment.scheduleCancelable(runAt);
```

Every generated method supports an awaited call plus `.trigger`, `.schedule`,
and `.scheduleCancelable`. No npm install, package path dependency, manual
bridge-generation command, or REST client `configure()` call is needed:
`golem build` regenerates the bridge before the consumer compiles.

## 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-ts` skill.

