Skip to content
OpenComputer
Agent API

Agent hooks

Compose agent capabilities with reactive hooks

An agent function returns instructions. Hooks give that agent the model, tools, services, and context it needs to follow those instructions.

import { useInput, useModel, useTool } from "@opencomputer/agent";

export default function Agent() {
  const input = useInput();
  useModel("anthropic/claude-sonnet-4.6");

  if (input.text?.includes("latest")) {
    useTool("web-search");
  }

  return "Answer directly. Use current sources when web search is available.";
}

How hooks work

OpenComputer renders the exported function before each model step. Every hook called during that render contributes to the next step’s configuration. Hooks describe capabilities; they do not call the model or run a tool themselves.

Unlike React component hooks, OpenComputer resource hooks can be conditional. The example above exposes web-search only when the current input asks for recent information.

Hooks can only run while OpenComputer is rendering an agent. Keep network requests and other asynchronous work inside tools, not in the agent function.

Built-in hooks

HookWhat it doesGuide
useInput()Reads the current input source, text, and structured payloadInputs
useCurrentInput()Alias for useInput()Inputs
useModel(model)Selects the model used for the next stepModels
useTool(tool)Exposes a named or code-defined toolTools
useMcpServer(server)Exposes tools from a managed HTTPS MCP serverMCP servers
useSessionData<T>(key)Reads a durable value associated with the sessionSession data
useMemory(memory)Reads bound memory and selects its permitted tools for this model requestMemory

Skills are also agent capabilities, but they are discovered from the agent’s skills/ directory rather than attached with a hook.

Compose your own hooks

Hooks are ordinary synchronous functions. Group related capabilities in a custom hook when several agents use the same setup:

import { useTool } from "@opencomputer/agent";

function useGitHubResearch() {
  useTool("github-search");
  return "Check GitHub before making claims about repositories or issues.";
}

export default function Agent() {
  const githubInstructions = useGitHubResearch();
  return `Research the request carefully. ${githubInstructions}`;
}

Custom hooks keep agent functions readable and make capability bundles reusable. Prefix them with use so their purpose is obvious.

Next steps