Agentic Usage

Patterns for planning, tool execution, and iterative response loops.

Agent Loop Pattern

TypeScript
import { TheRouter.ai } from "@therouter/sdk";

const client = new TheRouter.ai({
  apiKey: process.env.THEROUTER_API_KEY!,
  baseURL: "https://api.therouter.ai/v1",
});

let turnInput = "Create a rollout plan and call tools when needed.";

for (let step = 0; step < 6; step += 1) {
  const response = await client.responses.create({
    model: "anthropic/claude-sonnet-4.5",
    input: turnInput,
    tools: [
      {
        type: "function",
        function: {
          name: "get_release_window",
          description: "Return approved maintenance windows",
          parameters: {
            type: "object",
            properties: { region: { type: "string" } },
            required: ["region"],
          },
        },
      },
    ],
  });

  if (!response.output?.some((item) => item.type === "tool_call")) {
    console.log(response.output_text);
    break;
  }

  turnInput = "Tool output: us-east-2 window starts at 02:00 UTC.";
}

Agent Response Shape

NameTypeRequiredDescription
output
arrayRequiredOrdered output items emitted this turn.
output[].type
stringRequiredmessage | tool_call | reasoning
output_text
stringFlattened plain text from message items.
id
stringRequiredStable response ID for tracing and retries.

Practical Guardrails

agent-guardrails.ts
const MAX_STEPS = 8;

if (step >= MAX_STEPS) {
  throw new Error("Agent exceeded max steps");
}

if (!response.output_text && !response.output?.length) {
  throw new Error("No assistant output returned");
}
Deterministic tools
Keep tool contracts narrow and deterministic. Return normalized payloads so the next model turn can reason on stable structure.