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

# Restate services as tools

> Let the model call the handlers of your Restate services, with each tool call as a durable call to the handler.

Any Restate handler can be a tool. Each tool call is a durable call to the handler, which runs as its own invocation, with its own journal and retries.

`handlerTool` exposes one handler with the arguments you define. `serviceTool` exposes every handler of a service, and the model chooses the handler:

```typescript {"CODE_LOAD::harness/src/tools.ts#handler_tools"}  theme={null}
const orderAgent = agent<Order>({
  model,
  tools: {
    // One handler, with the arguments you choose.
    sendEmail: handlerTool({
      description: "Send an email to the customer.",
      input: z.object({to: z.string(), body: z.string()}),
      call: (input) => restate.client(emails).send(input),
    }),
    // Every handler of a service, from its interface.
    refunds: serviceTool(refundIface),
    reserve,
  },
});
```

## Service interfaces

`serviceTool` reads the handler names, descriptions, and input schemas from a `restate.iface` interface.
An interface declares the handlers of a service without their implementation, so your agent's service doesn't need to import the code of the service that it calls.
The called service implements the interface with `restate.implement`. To expose a single handler, pass `{iface, handler}`:

```typescript {"CODE_LOAD::harness/src/tools.ts#iface"}  theme={null}
// The interface declares the handlers with their schemas and descriptions.
export const refundIface = restate.iface.service("RefundService", {
  getPolicy: restate.iface.schemas({
    input: z.object({orderId: z.string()}),
    output: z.object({refundWindowDays: z.number()}),
    description: "Read the refund policy for an order.",
  }),
  issueRefund: restate.iface.schemas({
    input: z.object({orderId: z.string(), amountCents: z.number().int().positive()}),
    output: z.object({refundId: z.string()}),
    description: "Issue a refund for an order. The amount is in cents.",
  }),
});

// The refund service implements the interface.
export const refundService = restate.implement(refundIface, {
  handlers: {
    *getPolicy() {
      return {refundWindowDays: 30};
    },
    *issueRefund({orderId}) {
      return {refundId: `re_${orderId}`};
    },
  },
});

// A tool for the issueRefund handler only
const issueRefund = serviceTool({iface: refundIface, handler: "issueRefund"});
```

* For a Virtual Object or a Workflow, declared with `restate.iface.object` or `restate.iface.workflow`, also pass a `key`: a string, or a function that returns the key for the handler input.
* Restate Harness checks only that the model names a handler of the service. The called handler validates the input against the input schema of its `iface.schemas`. If the input doesn't match, the call fails, and the model receives the error.
* Interrupting the run cancels the invocation of a running tool call.
* If the handler throws a `ToolError`, the model receives its code. This requires Restate Server 1.6 or later. See [What the model receives](https://docs.restate.dev/harness/error-handling.md#what-the-model-receives). Configure retries with the [retry policy](https://docs.restate.dev/services/configuration.md#retries) of the called handler.


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