Coordination
Memory Events
Reactive programming with memory change subscriptions — trigger agent logic when shared state changes.
Trigger agent logic the instant shared state changes -- no polling, no delays.
Agents often need to react when another agent writes data: reorder inventory when stock drops, alert a human when risk spikes, kick off downstream processing when upstream results land. AgentField's memory event system lets you subscribe to key patterns and run handlers when matching keys change -- across any scope, from any agent.
from agentfield import Agent
app = Agent(node_id="inventory-monitor")
# Reactive inventory — auto-reorder when stock drops below threshold
@app.on_change("warehouse.stock.*")
async def on_stock_change(event):
product_id = event.key.split(".")[-1]
stock_level = event.data.get("quantity", 0)
threshold = event.data.get("reorder_threshold", 10)
if stock_level < threshold:
await app.call(
"procurement.reorder",
product_id=product_id,
current_stock=stock_level,
order_quantity=threshold * 3,
)
await app.memory.global_scope.set(
f"alerts.reorder.{product_id}",
{"status": "reorder_triggered", "quantity": threshold * 3},
)
# Cross-agent notification — alert when any agent flags high risk
@app.memory.global_scope.on_change("risk.scores.*")
async def on_risk_spike(event):
if event.data.get("level") == "critical":
await app.call(
"notifications.slack",
channel="#risk-alerts",
message=f"Critical risk: {event.key} = {event.data}",
)import { Agent } from "@agentfield/sdk";
const agent = new Agent({ nodeId: "inventory-monitor" });
// Reactive inventory — auto-reorder when stock drops below threshold
agent.watchMemory("warehouse.stock.*", async (event) => {
const productId = event.key.split(".").pop()!;
const stockLevel = event.data?.quantity ?? 0;
const threshold = event.data?.reorderThreshold ?? 10;
if (stockLevel < threshold) {
await agent.call("procurement.reorder", {
productId,
currentStock: stockLevel,
orderQuantity: threshold * 3,
});
await agent.memory.globalScope.set(
`alerts.reorder.${productId}`,
{ status: "reorder_triggered", quantity: threshold * 3 },
);
}
});
// Cross-agent notification — alert when any agent flags high risk
agent.watchMemory("risk.scores.*", async (event) => {
if (event.data?.level === "critical") {
await agent.call("notifications.slack", {
channel: "#risk-alerts",
message: `Critical risk: ${event.key} = ${JSON.stringify(event.data)}`,
});
}
}, { scope: "global" });What just happened
- A memory write became an event source for downstream automation
- Handlers reacted immediately without polling loops
- The same reactive pattern worked across agents because events flow through shared memory
Example event payload:
{
"key": "warehouse.stock.widget-42",
"scope": "global",
"scope_id": "global",
"action": "set",
"data": { "quantity": 3, "reorder_threshold": 10 },
"previous_data": { "quantity": 11, "reorder_threshold": 10 }
}