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

# Order a service

> Build two schema tools that let a clinician order a lab test, imaging study, or nursing intervention: the agent searches your catalog of orderable services and prepares the order for the clinician to confirm.

The clinician orders a service, a step EHRs call computerized provider order entry (CPOE). The agent searches your facility's catalog of orderable services, selects the service, completes the order's mandatory fields, and prepares the order for the clinician, who confirms before anything is placed. Reading existing orders is a different flow: use the `ServiceRequest` resource type of the tool in [Retrieve patient data](https://docs.corti.ai/agentic/cookbook/retrieve.md).

![CPOE order lifecycle: the clinician asks to order, the agent searches the catalog, selects the service, completes the mandatory fields, and prepares the order. The clinician confirms before the order is placed.](https://mintcdn.com/corti/OdqulwjO6Fo6Jlod/images/recipes-order-light.svg?fit=max\&auto=format\&n=OdqulwjO6Fo6Jlod\&q=85\&s=9c59275e463662ec716e2b7393bff5f1)

The CPOE lifecycle: search, select, complete, and prepare. The clinician confirms before anything is placed.

This page builds on [Build a schema tool](https://docs.corti.ai/agentic/cookbook/extract.md#build-a-schema-tool), which explains the five fields of the connector body, `additionalProperties: false`, `required`, `const`, `enum`, `transition`, and how to attach a tool to your agent. What is new here: two tools that chain through the client, `anyOf` branches tagged by an `action` field, values that are both required and nullable, and a search that ends in `input_required` followed by an order tool that ends in `complete`.

## Build the order tools

The target: the clinician asks for a CRP, and the agent searches the catalog, adds C-Reactive Protein to an order, fills the order's mandatory fields, and prepares the order for the clinician to review. Ordering works against one order, identified by an `orderId`.

### 1. Split the job into two tools that chain

`search_cpoe_catalog` finds candidate services in the catalog, and `make_cpoe_order` acts on the order. Each is a complete schema tool with its own `transition`:

```json theme={null}
{ "type": "schema", "name": "search_cpoe_catalog", "description": "...", "schema": { ... }, "transition": "input_required" }
{ "type": "schema", "name": "make_cpoe_order", "description": "...", "schema": { ... }, "transition": "complete" }
```

The tools chain through the client. The agent calls the search, which emits a search request and returns no results inline. The client resolves the search against your catalog and returns the candidates, with the `orderId`, in a new message on the same `contextId`, as described in [Send a message to an agent](https://docs.corti.ai/agentic/guides/send-message.md). The agent then calls `make_cpoe_order` with fields copied from those candidates.

The `transition` is declared per connector, not per action. So **every** call to `make_cpoe_order` ends the run, `select` included, and the client sends a new message to start the run that performs the next action.

### 2. Put the rule for a field in its description

The search schema is a `requests` array, one entry per distinct service the clinician asked for. A `description` can sit on any property, not only on the tool, and the model reads it when it fills that field:

```json theme={null}
"orderId": { "type": "string", "description": "Omit for a new order. Include when an active order exists, and use the same value in every entry of the call." },
"keywords": { "type": "array", "minItems": 1, "description": "Short, lowercase search terms for this one service. Keep them terse, and prefer single words where your catalog search tokenizes.", "items": { "type": "string" } }
```

`orderId` is not in `required`, so its description carries the rule for when to send it. `minItems: 1` on both `requests` and `keywords` means every search carries at least one entry, and every entry at least one term.

### 3. Give each action its own shape, tagged by action

The order schema is an `items` array, and each item is one of five actions: `select`, `complete`, `prepare-for-review`, `remove`, or `cancel`. The actions carry different fields, so `items` is an `anyOf` with one closed branch per action. Each branch requires an `action` field pinned by a one-value `enum`, which tells the client which branch an item matched:

```json theme={null}
{
  "type": "object",
  "additionalProperties": false,
  "required": ["action", "orderId"],
  "description": "Hand the finished order to the clinician to review and confirm.",
  "properties": {
    "action": { "enum": ["prepare-for-review"] },
    "orderId": { "type": "string" }
  }
}
```

`anyOf` checks each item on its own, so the schema cannot require that all items in one call share one action. That rule goes in the tool's `description` instead: "All items in a single call must be the same action."

The `complete` action fills the order's mandatory fields. It is unrelated to the `complete` transition, which ends the run after every action.

### 4. Copy service fields from the search response

`select` and `remove` carry a `services` array, and every field in it comes from the search response. The agent does not compose them:

```json theme={null}
"required": ["id", "title", "code", "category", "sourceSystem"],
"properties": {
  "id": { "type": "string" },
  "title": { "type": ["string", "null"], "description": "For traceability only." },
  "code": {
    "type": "object", "additionalProperties": false,
    "required": ["system", "code"],
    "properties": {
      "system": { "type": "string" },
      "code": { "type": "string" }
    }
  },
  "category": { "enum": ["laboratory", "imaging", "nursing", "general"] },
  "sourceSystem": { "type": "string" }
}
```

This is where the two tools chain. The client resolves a selected service by matching on its `id`, not on the `title`, which is carried for traceability only. `code` pairs the service's code with the `system` it belongs to. `category` is example vocabulary: yours would be your own catalog's top-level split. `sourceSystem` names the back-end system that holds the catalog entry, such as `central-lab`, and the client uses it to look the service up.

### 5. Require every key, and allow null

`complete` carries a `values` array, one entry per mandatory field value. All four keys are required, and two of them accept `null` through the type `["string", "null"]`:

```json theme={null}
"required": ["property", "serviceId", "targetId", "value"],
"properties": {
  "property": { "type": "string" },
  "serviceId": { "type": ["string", "null"] },
  "targetId": { "type": ["string", "null"] },
  "value": { "type": "string" }
}
```

Required and nullable together means the agent always states `serviceId` and `targetId`, either with a value or with an explicit `null`. The client never has to guess whether a missing key means "empty" or "forgotten". `serviceId` is the `id` of a service already on the order. `title` in `services[]` follows the same required-and-nullable pattern.

## The full bodies

Attach both schema tools to one agent, in the same `connectors` array. Order needs no registry connector, so the array holds only these two bodies. [Attach it with the coding connector](https://docs.corti.ai/agentic/cookbook/extract.md#attach-it-with-the-coding-connector) shows the `connectors` array and where to send it.

### The search tool

```json expandable theme={null}
{
  "type": "schema",
  "name": "search_cpoe_catalog",
  "description": "Search the CPOE catalog for one or more orderable services. Emit one request entry per distinct service the clinician named. Keep keywords short and lowercase, and prefer single words where your catalog search tokenizes. The tool emits the search request and does not return results inline. Ends this run: the client returns the candidates in a new message.",
  "schema": {
    "type": "object",
    "additionalProperties": false,
    "required": ["requests"],
    "properties": {
      "requests": {
        "type": "array",
        "minItems": 1,
        "description": "One entry per distinct service the clinician asked for. All entries in one call link to one order.",
        "items": {
          "type": "object",
          "additionalProperties": false,
          "required": ["keywords"],
          "properties": {
            "orderId": { "type": "string", "description": "Omit for a new order. Include when an active order exists, and use the same value in every entry of the call." },
            "keywords": { "type": "array", "minItems": 1, "description": "Short, lowercase search terms for this one service. Keep them terse, and prefer single words where your catalog search tokenizes.", "items": { "type": "string" } }
          }
        }
      }
    }
  },
  "transition": "input_required"
}
```

| Field | Type | Required | Description |
| - | - | - | - |
| `requests` | array, at least one item | yes | One entry per distinct service the clinician asked for. All entries in one call link to one order |
| `requests[].keywords` | array of strings, at least one item | yes | Short, lowercase search terms for this one service |
| `requests[].orderId`\* | string | no | Omit for a new order. Include when an active order exists, with the same value in every entry of the call |

\* Differs slightly from [FHIR R4](https://docs.corti.ai/agentic/cookbook/overview#based-on-fhir-r4), where a simpler field is easier for the agent to fill.

### The order tool

```json expandable theme={null}
{
  "type": "schema",
  "name": "make_cpoe_order",
  "description": "Drive a CPOE order through its lifecycle. Each call is one action against one orderId: select, complete, prepare-for-review, remove, or cancel. All items in a single call must be the same action. Ends this run: the client sends the next action as a new message. Nothing is placed until the clinician confirms.",
  "schema": {
    "type": "object",
    "additionalProperties": false,
    "required": ["items"],
    "properties": {
      "items": {
        "type": "array",
        "minItems": 1,
        "description": "CPOE order actions. Each item is one action. All items in one call must share one action.",
        "items": {
          "anyOf": [
            {
              "type": "object",
              "additionalProperties": false,
              "required": ["action", "orderId", "services"],
              "description": "Add services to the order.",
              "properties": {
                "action": { "enum": ["select"] },
                "orderId": { "type": "string" },
                "services": {
                  "type": "array", "minItems": 1,
                  "description": "The services to add, from the search response.",
                  "items": {
                    "type": "object", "additionalProperties": false,
                    "required": ["id", "title", "code", "category", "sourceSystem"],
                    "properties": {
                      "id": { "type": "string" },
                      "title": { "type": ["string", "null"], "description": "For traceability only." },
                      "code": {
                        "type": "object", "additionalProperties": false,
                        "required": ["system", "code"],
                        "properties": {
                          "system": { "type": "string" },
                          "code": { "type": "string" }
                        }
                      },
                      "category": { "enum": ["laboratory", "imaging", "nursing", "general"] },
                      "sourceSystem": { "type": "string" }
                    }
                  }
                }
              }
            },
            {
              "type": "object",
              "additionalProperties": false,
              "required": ["action", "orderId", "values"],
              "description": "Fill mandatory fields on the order's services.",
              "properties": {
                "action": { "enum": ["complete"] },
                "orderId": { "type": "string" },
                "values": {
                  "type": "array", "minItems": 1,
                  "description": "The mandatory field values to supply.",
                  "items": {
                    "type": "object", "additionalProperties": false,
                    "required": ["property", "serviceId", "targetId", "value"],
                    "properties": {
                      "property": { "type": "string" },
                      "serviceId": { "type": ["string", "null"] },
                      "targetId": { "type": ["string", "null"] },
                      "value": { "type": "string" }
                    }
                  }
                }
              }
            },
            {
              "type": "object",
              "additionalProperties": false,
              "required": ["action", "orderId"],
              "description": "Hand the finished order to the clinician to review and confirm.",
              "properties": {
                "action": { "enum": ["prepare-for-review"] },
                "orderId": { "type": "string" }
              }
            },
            {
              "type": "object",
              "additionalProperties": false,
              "required": ["action", "orderId", "services"],
              "description": "Remove services from an active order.",
              "properties": {
                "action": { "enum": ["remove"] },
                "orderId": { "type": "string" },
                "services": {
                  "type": "array", "minItems": 1,
                  "items": {
                    "type": "object", "additionalProperties": false,
                    "required": ["id", "title", "code", "category", "sourceSystem"],
                    "properties": {
                      "id": { "type": "string" },
                      "title": { "type": ["string", "null"], "description": "For traceability only." },
                      "code": {
                        "type": "object", "additionalProperties": false,
                        "required": ["system", "code"],
                        "properties": {
                          "system": { "type": "string" },
                          "code": { "type": "string" }
                        }
                      },
                      "category": { "enum": ["laboratory", "imaging", "nursing", "general"] },
                      "sourceSystem": { "type": "string" }
                    }
                  }
                }
              }
            },
            {
              "type": "object",
              "additionalProperties": false,
              "required": ["action", "orderId"],
              "description": "Cancel the order.",
              "properties": {
                "action": { "enum": ["cancel"] },
                "orderId": { "type": "string" }
              }
            }
          ]
        }
      }
    }
  },
  "transition": "complete"
}
```

Each item in `items` is one of five actions against the shared `orderId`. All items in a single call must share one action. The happy path is `select`, then `complete`, then `prepare-for-review`. `remove` and `cancel` handle changes and abandonment.

| Action | What it does | Key fields |
| - | - | - |
| `select` | Add services to the order | `orderId`, `services[]` (each: `id`, `title`, `code`, `category`, `sourceSystem`) |
| `complete` | Fill mandatory fields on the order's services | `orderId`, `values[]` (each: `property`, `serviceId`, `targetId`, `value`) |
| `prepare-for-review` | Hand the finished order to the clinician to review and confirm | `orderId` only |
| `remove` | Remove services from an active order | `orderId`, `services[]` (same five fields as `select`) |
| `cancel` | Cancel the order | `orderId` only |

> [accordion] Service and value fields
>
> Each service in `services[]`, for `select` and `remove`. All five fields are copied from the search response.
>
> | Field | Type | Required | Description |
> | - | - | - | - |
> | `id` | string | yes | The client resolves the selected service by matching on this field, such as `CRP` in the example |
> | `title` | string or null | yes | Carried for traceability only, such as `C-Reactive Protein` |
> | `code` | object | yes | The service's code and the code system it belongs to |
> | `code.system` | string | yes | The code system the code belongs to, such as `local-catalog` |
> | `code.code` | string | yes | The code itself, such as `CRP-01` |
> | `category` | enum | yes | `laboratory`, `imaging`, `nursing`, `general`. The kind of service, which lets the client route the order. Example vocabulary: use your catalog's top-level split |
> | `sourceSystem`\* | string | yes | The back-end system that holds the catalog entry, which the client uses to look the service up, such as `central-lab` |
>
> Each value in `values[]`, for `complete`:
>
> | Field | Type | Required | Description |
> | - | - | - | - |
> | `property` | string | yes | The mandatory order property, such as `priority`. Discover these from the order when the agent performs `complete` |
> | `serviceId` | string or null | yes | The `id` of the service the value belongs to, or `null` for a field on the whole order, as in the example |
> | `targetId` | string or null | yes | Which item of a list-valued property this value changes. `null` for every other property |
> | `value` | string | yes | The value to supply, such as `routine` |
>
> \* Differs slightly from [FHIR R4](https://docs.corti.ai/agentic/cookbook/overview#based-on-fhir-r4), where a simpler field is easier for the agent to fill.

## Point the system prompt at the tools

The schema tools define what the agent can emit. The system prompt routes each request to the right tool and carries the lifecycle. This is the minimal prompt for order:

```text theme={null}
Role: prepare service orders (labs, imaging, interventions) for clinician confirmation, from a clinician's request.

You have two tools: search_cpoe_catalog (find candidates in the catalog) and make_cpoe_order (act on the order: select, complete, prepare-for-review, remove, cancel).

Route each request:
- asks to order a service, no candidates yet    -> search_cpoe_catalog
- has candidates or an active order             -> make_cpoe_order, next action
- asks to remove a service or cancel the order  -> make_cpoe_order with remove or cancel
- something else                                -> say you handle orders only

Advance the order one action per call, in lifecycle order: search, select, complete, prepare-for-review.
```

Add [the rules every flow shares](https://docs.corti.ai/agentic/cookbook/overview#write-the-system-prompt). Order is the flow likeliest to break the first of them, one tool call and never two: every call to `make_cpoe_order` ends the run, so the agent must not mix actions, or skip ahead, within one call's `items`. If the agent stalls mid-lifecycle, check that the client sends the next message after each action before adding prompt rules.

> [accordion] Run it: flow, example, and the client's apply-step
>
> A clinician asks for a CRP. The agent walks the order through its lifecycle: search the catalog, select services, complete their mandatory fields, and prepare the order for the clinician to review. The system prompt and the two tools' descriptions produce this sequence. Each step below is one run: the call ends the run, and the client sends the next message.
>
> **1. Search the catalog.** The agent calls `search_cpoe_catalog` with one entry per distinct service, each with short lowercase `keywords`. This is a new order, so the entry has no `orderId`. The transition is `input_required`, and the client returns the candidates, with the `orderId`, in the next message.
>
> ```json theme={null}
> { "requests": [ { "keywords": ["crp", "c-reactive"] } ] }
> ```
>
> **2. Select services into the order.** The agent calls `make_cpoe_order` with action `select`: the item lists each chosen service in `services[]`, with the identifying fields copied from the search response, and carries the `orderId` returned with the search results.
>
> ```json theme={null}
> {
>   "items": [
>     {
>       "action": "select",
>       "orderId": "<order-id>",
>       "services": [
>         {
>           "id": "CRP",
>           "title": "C-Reactive Protein",
>           "code": { "system": "local-catalog", "code": "CRP-01" },
>           "category": "laboratory",
>           "sourceSystem": "central-lab"
>         }
>       ]
>     }
>   ]
> }
> ```
>
> **3. Complete the mandatory fields.** The agent calls `make_cpoe_order` with action `complete`, supplying the mandatory `values` for each service on the order.
>
> ```json theme={null}
> {
>   "items": [
>     {
>       "action": "complete",
>       "orderId": "<order-id>",
>       "values": [
>         { "property": "priority", "serviceId": null, "targetId": null, "value": "routine" }
>       ]
>     }
>   ]
> }
> ```
>
> **4. Prepare the order for review.** The agent calls `make_cpoe_order` with action `prepare-for-review`. This is the terminal action: it hands the finished order to the clinician, who reviews it, confirms, and places it. The agent places nothing.
>
> ```json theme={null}
> {
>   "items": [
>     { "action": "prepare-for-review", "orderId": "<order-id>" }
>   ]
> }
> ```
>
> Two paths branch off the happy path. `remove` takes services back out of an active order when the clinician changes their mind before completion. `cancel` abandons the order altogether. Both are single actions against the same `orderId`, and both end the run.
>
> The client owns the apply-step at every point where the agent stops:
>
> * **After the search**, the client resolves the keywords against your catalog and returns the candidates, with the `orderId`, in a new message on the same `contextId`. A task in `input_required` is not finished: the new message resumes it, as described in [Task lifecycle](https://docs.corti.ai/agentic/task-lifecycle.md).
> * **After each order action**, the client applies the action to the order, resolving each selected service by its `id`, and sends the next message. After `select`, that message lists the order's mandatory properties still to fill. The run that message starts performs the next action.
> * **After `prepare-for-review`**, the client shows the finished order for review, and places it through your order system only when the clinician confirms.

## Adapt it

* Replace the `category` enum with your own catalog's top-level categories.
* Do not hardcode mandatory properties such as `priority` in the schema. Discover them from the order when the agent performs `complete`.
* Tune the `keywords` guidance in the search tool's `description` and field description to how your catalog search tokenizes.
* Edit both tools' `description` prose to match how your clinicians ask for orders.
* Keep the transitions: `input_required` on the search, so the client can return the candidates, and the `complete` transition on the order tool, so each action is one run and nothing is placed until the clinician confirms.


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