> ## Documentation Index
> Fetch the complete documentation index at: https://phaseo.app/docs/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> Phaseo Gateway base URL: https://api.phaseo.app/v1. Send `Authorization: Bearer $PHASEO_API_KEY` with every request.
> Model IDs look like `provider/model`, for example `openai/gpt-6-astra`. Do not guess IDs: list them with `GET /v1/models` or check https://phaseo.app/models. `phaseo/free` routes to an available free model and needs no wallet balance.
> For new text integrations prefer `POST /v1/responses`. `POST /v1/chat/completions` (OpenAI format) and `POST /v1/messages` (Anthropic format) suit existing code.
> The full API contract is https://phaseo.app/docs/openapi/v1/openapi.yaml. Official SDKs: `@phaseo/sdk` (TypeScript) and `phaseo` (Python), plus Go, C#, PHP, Ruby and Rust.
> Search these docs over MCP at https://phaseo.app/docs/mcp, and install Phaseo skills with `npx skills add https://phaseo.app/docs`. If a page is wrong or out of date, report it with the MCP `submit_feedback` tool.
> Manage Phaseo accounts with the `phaseo` CLI (`npm install -g @phaseo/cli`, then `phaseo login` or `phaseo login --device-code`), the `/v1` Management API or the MCP server at `https://mcp.phaseo.app/mcp?access=manage`. Do not send users to the dashboard for account tasks. A person is still needed for sign-up, approving a sign-in, paying the first top-up on the checkout link that `phaseo credits add` returns when no card is saved, confirming each saved-card top-up from the emailed link, and account security (password, MFA, passkeys). Create API keys with `phaseo keys create --name <name> --json`; never guess one. Credit top-ups need `phaseo login --scopes billing:write` first. Task map: https://phaseo.app/docs/v1/developers/manage-from-an-agent.md.

# Build a durable agent loop in TypeScript

> Run a resumable agent loop on top of Phaseo Gateway with the TypeScript Agent SDK.

Use this recipe when you want more than a one-shot text request:

* multi-step tool loops
* local runtime tools
* resumable runs
* a single gateway-backed entrypoint for agent work

## 1. Install the SDKs

```bash npm theme={null}
npm install @phaseo/sdk @phaseo/agent-sdk
```

```bash pnpm theme={null}
pnpm add @phaseo/sdk @phaseo/agent-sdk
```

```bash yarn theme={null}
yarn add @phaseo/sdk @phaseo/agent-sdk
```

```bash bun theme={null}
bun add @phaseo/sdk @phaseo/agent-sdk
```

## 2. Define one small runtime tool

Keep the first tool deterministic and easy to inspect.

```ts theme={null}
import { defineTool } from "@phaseo/agent-sdk";

export const lookupDocs = defineTool({
  id: "lookup-docs",
  description: "Look up internal docs by slug.",
  async execute(input: { slug: string }) {
    return {
      slug: input.slug,
      url: `https://phaseo.app/docs/v1/${input.slug}`,
    };
  },
});
```

## 3. Create the agent

```ts theme={null}
import { createAgent } from "@phaseo/agent-sdk";
import { lookupDocs } from "./tools";

export const supportDocsAgent = createAgent({
  id: "support-docs-agent",
  model: "phaseo/free",
  instructions: "Use tools when helpful and finish with a concise answer.",
  tools: [lookupDocs],
});
```

## 4. Use the gateway-backed adapter

```ts theme={null}
import {
  createGatewayAgentClient,
} from "@phaseo/agent-sdk";
import { supportDocsAgent } from "./agent";

const result = await supportDocsAgent.run({
  input: "Find the docs page for presets and explain when to use them.",
  client: createGatewayAgentClient({
    clientOptions: {
      apiKey: process.env.PHASEO_API_KEY!,
    },
  }),
});

console.log(result.output);
```

## 5. What persists between steps

Each completed step updates returned run state with:

* the run id
* the message history
* any returned tool calls
* any local tool results
* the final output when the run completes

If you want resumability across requests or processes, persist that returned value in your own application and pass it back into `continueRun()` later.

## 6. When to keep the first version simple

* one agent per workflow
* application-owned serialization before any cross-request resume flow
* a small tool list before generic tool registries
* one log line per run with the run id and gateway request id

## 7. When to move beyond this recipe

Move past the first durable loop when you need:

* approval pauses
* application-owned persistence for returned run state
* multiple agent definitions with shared runtime tools

At that point, keep the same gateway-backed adapter and evolve the store and orchestration around it.

## Related guides

* [TypeScript Agent SDK](https://phaseo.app/docs/v1/sdk-reference/typescript/agent-sdk.md)
* [Presets](https://phaseo.app/docs/v1/guides/presets.md)
* [Routing and fallbacks](https://phaseo.app/docs/v1/guides/routing-and-fallbacks.md)
* [Set up coding agents with SKILL.md](https://phaseo.app/docs/v1/cookbook/coding-agents-skill-md.md)
* [Launch on the free router](https://phaseo.app/docs/v1/cookbook/free-router-first-deploy.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.