POST
/
v1
/
experimental
/
templatize_prompt

提示詞工具 API 目前處於封閉研究預覽階段。申請加入封閉研究預覽

開始之前

提示詞工具是一組用於生成和改進提示詞的 API。與我們的其他 API 不同,這是一個實驗性 API:您需要申請訪問權限,並且它不像其他 API 那樣對長期支持有相同程度的承諾。

這些 API 與 Anthropic Workbench 中提供的功能類似,旨在供其他提示詞工程平台和實驗環境使用。

開始使用提示詞改進工具

要使用提示詞生成 API,您需要:

  1. 已加入提示詞工具 API 的封閉研究預覽
  2. 直接使用 API,而不是 SDK
  3. 添加測試版標頭 prompt-tools-2025-04-02

此 API 在 SDK 中不可用

提示詞模板化

Headers

anthropic-beta
string[]

Optional header to specify the beta version(s) you want to use.

To use multiple betas, use a comma separated list like beta1,beta2 or specify the header multiple times for each beta.

x-api-key
string
required

Your unique API key for authentication.

This key is required in the header of all API requests, to authenticate your account and access Anthropic's services. Get your API key through the Console. Each key is scoped to a Workspace.

Body

application/json
messages
object[]
required

The prompt to templatize, structured as a list of message objects.

Each message in the messages array must:

  • Contain only text-only content blocks
  • Not include tool calls, images, or prompt caching blocks

Example of a simple text prompt:

[
  {
    "role": "user", 
    "content": [
      {
        "type": "text",
        "text": "Translate hello to German"
      }
    ]
  }
]

Note that only contiguous user messages with text content are allowed. Assistant prefill is permitted, but other content types will cause validation errors.

system
string | null

The existing system prompt to templatize.

{
  "system": "You are a professional English to German translator",
  [...]
}

Note that this differs from the Messages API; it is strictly a string.

Response

200 - application/json
messages
object[]
required

The templatized prompt with variable placeholders.

The response includes the input messages with specific values replaced by variable placeholders. These messages maintain the original message structure but contain uppercase variable names in place of concrete values.

For example, an input message content like "Translate hello to German" would be transformed to "Translate {{WORD_TO_TRANSLATE}} to {{TARGET_LANGUAGE}}".

{
  "messages": [
    {
      "role": "user",
      "content": [
        {
          "type": "text",
          "text": "Translate {{WORD_TO_TRANSLATE}} to {{TARGET_LANGUAGE}}"
        }
      ]
    }
  ]
}
system
string
required

The input system prompt with variables identified and replaced.

If no system prompt was provided in the original request, this field will be an empty string.

usage
object
required

Usage information

variable_values
object
required

A mapping of template variable names to their original values, as extracted from the input prompt during templatization. Each key represents a variable name identified in the templatized prompt, and each value contains the corresponding content from the original prompt that was replaced by that variable.

Example:

"variable_values": {
  "WORD_TO_TRANSLATE": "hello",
  "TARGET_LANGUAGE": "German"
}

In this example response, the original prompt – Translate hello to German – was templatized to Translate WORD_TO_TRANSLATE to TARGET_LANGUAGE, with the variable values extracted as shown.