

# Customize retrieval instructions

Retrieval instructions tell the Context retrieval agent how to answer the queries that your end users and agents send. They cover which sources to trust, what your terms mean, what to cite, and what to avoid. The *Context retrieval agent* is the agent that AWS Context runs to answer agentic retrieval requests. Each Agent Context has at most one set of retrieval instructions, written in plain text or Markdown.

 AWS Context stores the instructions in the Agent Context as a node, and every change to them is a changeset. As with a manual edit, the curation policies linked to the Agent Context decide whether a change applies at once or waits for review.

## Where retrieval instructions apply
<a name="retrieval-instructions-scope"></a>

The Context retrieval agent reads the instructions on every agentic retrieval request and applies them to its answer. This includes every `AgenticRetrieveStream` request, whether you call the operation directly or through the `context___AgenticRetrieveStream` tool of the Model Context Protocol (MCP) endpoint. For more information about agentic retrieval, see [Agentic retrieval](agentic-retrieval.md).

When you run an evaluation on the Agent Context, the Context retrieval agent applies the instructions to the answers that the evaluation checks. For more information about evaluations, see [Evaluating context](evaluating-context.md).

Retrieval instructions affect only the answers from agentic retrieval. `SearchAgentContext` and `TraverseAgentContext` don’t use them, and return your data as it is stored.

Retrieval instructions shape how the Context retrieval agent answers. They don’t change what a caller can access: every request still returns only the data that the caller is authorized to see.

If AWS Context can’t read the instructions for a request, you still get an answer to that request, without the instructions applied.

The instructions are part of the Agent Context, so anyone who can read the Agent Context can find them, for example with `SearchAgentContext`. Don’t put secrets or personal data in the instructions.

## Write effective instructions
<a name="retrieval-instructions-write"></a>

Write short, direct rules that apply to every query. Good instructions tell the Context retrieval agent:
+ Which sources to prefer when they disagree.
+ What your business terms mean.
+ What to include in an answer, and what to leave out.
+ What to do when the data doesn’t contain the answer.

The following example uses Markdown:

```
# Rules for every query
- For any question about employees, include only those with status "active".
- If two sources disagree, use the most recent one.
- Keep every answer to 2–3 sentences.
- If you can't find the answer in the data, say so. Don't guess.
```

The instructions must meet these rules:
+ They are 1–2,048 characters long. Length is counted in Unicode code points, so most emoji count as one character.
+ They contain no placeholder in square or curly brackets that starts with an upper-case letter and contains only upper-case letters, digits, and underscores, such as `[CUSTOMER_ID]` or `{REGION}`. Markdown with such text in brackets also matches this rule, for example the link `[FAQ](https://example.com)` or the task item `- [X] Done`.
+ They contain none of the tags that AWS Context reserves, in any letter case and with or without a slash or spaces, for example `<strategies>` or `</ Format_Rules >`. The reserved tag names are `strategies`, `format_rules`, `examples`, `definition`, `id`, `long_term_memory`, `space_metadata`, and `user_context`.

 AWS Context rejects a change that breaks these rules. If you save text that contains only white space through the API, the Context retrieval agent treats it as no instructions.

## Create, edit, and delete instructions in the console
<a name="retrieval-instructions-console"></a>

The **Retrieval instructions** page shows the current instructions and when they were last updated. If the Agent Context has no instructions, the page shows **No retrieval instructions**. To view the page, you need permission for `context:SearchAgentContext`. To save or delete instructions, you also need `context:CreateChangeSet`. To see that a change is waiting for review, you also need `context:ListCurationProposals`.

If you changed the instructions recently, it can take a few minutes for the change to show on the page. This applies to a change made somewhere else, such as one accepted on the **Curation** tab or made through the API. A change that you save on the page shows there right away.

**To open the retrieval instructions**

1. Open the AWS Context console, and then open your Agent Context.

1. From **Actions**, choose **Customize context**.

1. Choose **Customize retrieval instructions**.

**To create or edit the retrieval instructions**

1. Choose **Create instructions**, or choose **Edit** if the Agent Context already has instructions.

1. Enter the instructions. The character count shows how close you are to the 2,048-character limit.

1. (Optional) To start from a file, choose **Upload file**, and then choose a Markdown (MD) or plain text (TXT) file of up to 8 KB. The file’s text replaces the text in the editor. Review it before you save.

1. Choose **Save changes**.

If the change applies at once, the page shows **Instructions saved.** and the new text. If a curation policy holds the change, the page shows that the change is waiting for review. The current instructions stay in effect until a curator approves the change. For more information about review, see [Review changes and when they take effect](#retrieval-instructions-review).

To download the instructions, choose **Download**. The file is named `retrieval-instructions.md`.

**To delete the retrieval instructions**

1. Choose **Delete**.

1. Confirm the delete.

A delete is a change like any other, so a curation policy can hold it for review. After the delete applies, the page shows **Instructions deleted.** The Context retrieval agent then uses its default behavior. You can’t undo a delete, but you can create new instructions at any time.

## Review changes and when they take effect
<a name="retrieval-instructions-review"></a>

A change to the instructions is a changeset, so the curation policies linked to the Agent Context decide whether it applies at once or waits for review. This is true for saves and deletes alike.

The default curation policy for a new Agent Context holds only changes from trace analysis and ontology grounding. A change that you make to the instructions therefore applies at once.

To require review for changes to the instructions, add a curation policy that holds direct changes. A curation policy can test only the activity that generated a change, not the node types that the change touches. Every change that you make with `CreateChangeSet`, including saves and deletes on the **Retrieval instructions** page, is a direct change. The following policy therefore holds every change to the instructions, and every other direct change to the Agent Context.

```
forbid (
  principal,
  action == AWS::Context::Action::"PublishChangeSet",
  resource is AWS::Context::ChangeSet
)
when { resource has generatedBy && resource.generatedBy == AWS::Context::Activity::"DIRECT_MODIFICATION" };
```

If a change waits for review and you didn’t add such a policy, check the other policies linked to the Agent Context.

When a policy holds a change:

1. The **Retrieval instructions** page shows that the change is waiting for review, with a link to the **Curation** tab. The current instructions stay in effect.

1. A curator accepts or rejects the change on the **Curation** tab. To accept it, the curator needs permission for `context:AcceptCurationProposal`. For more information about reviewing proposals, see [Review proposals and quality signals](review-proposals.md).

1. After the change is accepted, it applies.

After a change applies, the next agentic retrieval request uses it, even if the **Retrieval instructions** page doesn’t show the change yet. For more information about this delay, see [Create, edit, and delete instructions in the console](#retrieval-instructions-console).

## Manage instructions with the API
<a name="retrieval-instructions-api"></a>

 AWS Context has no separate API for retrieval instructions. You write them with `CreateChangeSet`, as you would any manual edit, and you read them with `SearchAgentContext`. To write the instructions, you need permission for `context:CreateChangeSet`. To read them, you need `context:SearchAgentContext`.

The following table describes the node that holds the instructions.


| Item | Value | 
| --- | --- | 
| Node type |  `AWS::CONTEXT::RETRIEVAL_INSTRUCTION`  | 
| Node ID |  `https://ontology.context.aws.dev/retrievalInstruction:default`  | 
|  `retrievalInstructionId` (required) | Must be `default`. AWS Context rejects any other value. AWS Context builds the node ID from it, so every write goes to the same node. | 
|  `retrievalInstructionText` (required) | The instructions. For the rules the text must meet, see [Write effective instructions](#retrieval-instructions-write). | 
|  `createdAt`, `updatedAt` (optional) | Dates and times in ISO 8601 format. The console sets `updatedAt` on every save and shows it as the last updated time. | 

In the following examples, replace `<your Agent Context ID>` with the ID of your Agent Context.

To create or replace the instructions, create a changeset with one `update` operation on the node. If the node doesn’t exist, this request creates it. Otherwise it updates the properties that you send.

The following request creates or replaces the instructions. Send it to `POST /api/v1/agent-contexts/<your Agent Context ID>/change-sets`.

```json
{
  "operations": [
    {
      "update": {
        "entity": {
          "nodeId": "https://ontology.context.aws.dev/retrievalInstruction:default"
        },
        "properties": {
          "nodeType": { "stringValue": "AWS::CONTEXT::RETRIEVAL_INSTRUCTION" },
          "retrievalInstructionId": { "stringValue": "default" },
          "retrievalInstructionText": {
            "stringValue": "# Rules for every query\n- If two sources disagree, use the most recent one."
          }
        }
      }
    }
  ]
}
```

In this request:
+  `entity.nodeId` is the fixed ID of the instructions node. Use it as shown.
+ Each property value is wrapped in `stringValue`.
+  `retrievalInstructionText` is the new text of the instructions. It replaces the current text.

To make a retry safe, add a `clientToken` to the request body. A retry with the same token doesn’t create a second change. The AWS SDKs add a token for you.

To delete the instructions, create a changeset with one `delete` operation on the node. The following request deletes the instructions. Send it to the same path as the previous request.

```json
{
  "operations": [
    {
      "delete": {
        "entity": {
          "nodeId": "https://ontology.context.aws.dev/retrievalInstruction:default"
        }
      }
    }
  ]
}
```

The changeset’s `status` in the response tells you what happened. `PUBLISHED` means that the change applied. `PENDING` means that a curation policy holds it for review.

To read the instructions, search for the node by its ID. The following request reads the instructions. Send it to `POST /api/v1/search-agent-context`.

```json
{
  "agentContextId": "<your Agent Context ID>",
  "query": "retrieval instruction",
  "searchType": "SEMANTIC",
  "maxResults": 1,
  "filter": {
    "equalTo": {
      "key": "entityId",
      "value": { "stringValue": "https://ontology.context.aws.dev/retrievalInstruction:default" }
    }
  }
}
```

If the Agent Context has instructions, the response returns one item, and the text is in the item’s `properties.retrievalInstructionText.stringValue`. If it has none, the response returns no items. Search catches up with a change after a short delay, so a search right after a change can return the earlier text.