Skip to content
OpenComputer
Agent API

Models

Select a model with useModel

useModel() selects the model that powers the next agent step. OpenComputer handles provider authentication and response streaming.

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

export default function Agent() {
  useModel("anthropic/claude-sonnet-4.6");
  return "You are a helpful assistant. Be concise and practical.";
}

useModel() is a declaration. It returns nothing and does not make a model request itself.

Model identifiers

Use a provider/model string:

useModel("anthropic/claude-sonnet-4.6");

The object form is equivalent:

useModel({
  provider: "anthropic",
  model: "claude-sonnet-4.6",
});

Keep model selection in agent code so development and immutable deployments record the behavior together. Provider credentials are not added to source bundles or client applications.

Select a model conditionally

Because the agent re-renders for each model step, selection can respond to the current input:

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

export default function Agent() {
  const input = useInput();
  const needsDeepReview = input.text?.includes("deep review") ?? false;

  useModel(
    needsDeepReview
      ? "anthropic/claude-sonnet-4.6"
      : "anthropic/claude-haiku-4.5",
  );

  return needsDeepReview
    ? "Review the request carefully and explain important tradeoffs."
    : "Answer directly and briefly.";
}

Select one model for the next step and keep the choice predictable: model changes affect cost, latency, context limits, and output behavior.

Keep credentials out of agent code

Do not place provider API keys in agent.ts, tools, prompts, or the React application. OpenComputer authenticates the selected provider without adding the credential to agent code.

For credentials used by your own HTTP tools instead of model inference, use Secrets and outbound requests.

Use a connected model account

Organizations on Pro or Max can connect a Codex account and route eligible OpenAI models through it. Use the native openai provider when you want the connected account; Managed OpenRouter models continue to use the openrouter/provider/model form.

See Bring your own model account (BYOK) for setup, project enablement, billing behavior, and troubleshooting.