Documentation menu
Documentation

Inter-agent communication

Call a separately bound agent by durable name with typed RPC.

Start from a fresh project

This guide is self-contained. Create an empty Ayjnt project, then replace the starter agent with the files shown below. Run commands from the new project directory unless a step says otherwise.

terminal
bunx ayjnt new try-inter-agent --empty
cd try-inter-agent
bun install

# remove agents/alive once you are ready to add the guide's files

Use this.agent inside Ayjnt Agent

The protected this.agent() helper is Ayjnt’s typed wrapper around Cloudflare getAgentByName(). The target has its own top-level Durable Object namespace and storage.

agents/orders/agent.ts
import { Agent } from "ayjnt";
import InventoryAgent from "../inventory/agent";

export default class OrdersAgent extends Agent {
  async reserve(sku: string, quantity: number) {
    const inventory = await this.agent(InventoryAgent, "primary");
    return inventory.reserve(sku, quantity);
  }
}

Outside an Agent

Import getAgent<T>() from ayjnt when calling from a Worker handler or other framework code. Browser code should use callable methods instead of a Durable Object binding.

Internal agent RPC does not need @callable(). Public methods are callable through the typed Durable Object stub.

Run it and verify the result

Call the order agent once. Autocomplete should expose the inventory agent’s public methods, and both agent instances should preserve independent state.

terminal
bun run build
bun run dev

# the local app is now available at http://localhost:8787