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

# enforce

> One-shot contract execution

Runs a contract in a single call — schema, `RunFn`, and options in one shot. Returns a `ContractResult<T>`. Implemented on top of `defineContract`; behavior is identical.

## Signature

```typescript theme={null}
function enforce<T>(
  schema: ContractSchema<T>,
  run: RunFn,
  options: ContractOptions<T> & { name: string },
): Promise<ContractResult<T>>;
```

| Param     | Type                                    | Description                                            |
| --------- | --------------------------------------- | ------------------------------------------------------ |
| `schema`  | `ContractSchema<T>`                     | Zod v3 or v4 schema for the expected output            |
| `run`     | `RunFn`                                 | `(attempt) => Promise<string \| null>` — your LLM call |
| `options` | `ContractOptions<T> & { name: string }` | Rules, retry, logger, etc. `name` is required.         |

## `options`

Same shape as [`ContractConfig<T>`](/api-reference/define-contract#contractconfig-t) minus `schema`.

| Field          | Type                  | Default              | Description                                                                |
| -------------- | --------------------- | -------------------- | -------------------------------------------------------------------------- |
| `name`         | `string`              | *required*           | Human-readable identifier for the run — appears in logs, traces, dashboard |
| `rules`        | `Rule<T>[]`           | `[]`                 | Domain correctness rules                                                   |
| `retry`        | `RetryOptions`        | `{ maxAttempts: 3 }` | Retry behavior                                                             |
| `repairs`      | `RepairOverrides`     | —                    | Custom repair per category                                                 |
| `instructions` | `{ suffix?: string }` | —                    | Append text to the auto-generated prompt                                   |
| `onAttempt`    | `AttemptHook`         | —                    | Called after each attempt                                                  |
| `logger`       | `ContractLogger<T>`   | —                    | Lifecycle hooks (pass `createBoundaryLogger(...)` to ship events)          |
| `debug`        | `boolean`             | `false`              | Shorthand for `createConsoleLogger()`                                      |
| `model`        | `string`              | —                    | Stamped onto `BoundaryLogEvent.model` for this run                         |

## Example

```typescript theme={null}
import { z } from "zod";
import { enforce } from "@withboundary/contract";

const schema = z.object({
  tier: z.enum(["hot", "warm", "cold"]),
  score: z.number().min(0).max(100),
});

const result = await enforce(schema, async (attempt) => {
  const response = await callYourLLM({
    messages: [
      {
        role: "user",
        content: [
          "Score this lead as JSON.",
          attempt.instructions,
          leadSummary,
        ].join("\n\n"),
      },
      ...attempt.repairs,
    ],
  });

  return response.text;
}, {
  name: "lead-scoring",
  rules: [
    {
      name: "hot_requires_high_score",
      description: "Hot leads must have a score of at least 70",
      fields: ["tier", "score"],
      check: (lead) =>
        lead.tier !== "hot" || lead.score >= 70
          || `tier is "hot" but score is ${lead.score} (minimum 70 for hot)`,
    },
  ],
  retry: { maxAttempts: 3 },
});

if (result.ok) {
  console.log(result.data);
}
```

## When to use `enforce` vs `defineContract`

* **`enforce`** — one-off, ad-hoc, experimental. Inline definition and execution.
* **`defineContract`** — reusable, shared across endpoints, passed as a value.

See [enforce vs defineContract](/guides/enforce-vs-define) for a full comparison.

## See also

<CardGroup cols={2}>
  <Card title="defineContract" icon="cube" href="/api-reference/define-contract">
    Reusable contracts
  </Card>

  <Card title="Types" icon="code" href="/api-reference/types">
    Full type reference
  </Card>
</CardGroup>
