claude-fable-5-1
最后更新:2026-11-14
POSThttps://www.sagelib.com/chat/completions
claude-fable-5-1 模型的聊天生成接口,OpenAI兼容,支持多轮上下文、流式输出、工具调用、结构化输出等。
接口鉴权方式请参考:API接入与鉴权
请求参数
请求头
| 参数名称 | 描述 |
|---|---|
*Authorization string |
鉴权令牌,格式为 Bearer ${AIGATEWAY_TOKEN} |
*Content-Type string |
默认值:application/json 可选值:application/json 内容类型,固定为 application/json |
Body 参数
| 参数名称 | 描述 |
|---|---|
*model string |
可选值:"claude-fable-5-1" 模型名称,须精确匹配 |
*messages array |
可选值:content 对话消息列表(user/assistant/system),content 可为字符串或多模态块数组(text/image_url) |
max_tokens integer |
可选值:0 ~ 128000 生成 token 上限(旧名,含思考+回复文本) |
stop string |
可选值:Single string or string array 停止序列,仅非空白序列生效 |
stream boolean |
默认值:false 可选值:true、false 是否启用 SSE 流式输出 |
tools array |
可选值:Array of tool definitions 工具定义列表,OpenAI 格式(type: function,function 含 name/description/parameters) |
tool_choice string |
可选值:auto、none 工具选择策略 |
请求示例
curl -v 'https://www.sagelib.com/chat/completions' \
-H "Authorization: Bearer ***" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-fable-5-1",
"max_completion_tokens": 2048,
"messages": [
{
"role": "user",
"content": "What model are you? Which company made you? Briefly describe your capability boundaries."
}
]
}'
import json
import requests
# Configure base variables
base_url = "https://www.sagelib.com" # 把 base_url 换成你的网关地址 https://www.sagelib.com
api_key = "***" # Replace with your Bearer token
url = f"https://www.sagelib.com/chat/completions"
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json",
}
data = {
"model": "claude-fable-5-1",
"max_completion_tokens": 2048,
"messages": [
{
"role": "user",
"content": "What model are you? Which company made you? Briefly describe your capability boundaries."
}
]
}
# Send the POST request
response = requests.post(url, headers=headers, json=data)
# Print the status code and the returned JSON data
print(f"Status Code: {response.status_code}")
try:
print(json.dumps(response.json(), indent=2, ensure_ascii=False))
except Exception:
print(response.text)
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class LlmStreamClient {
public static void main(String[] args) {
String baseUrl = "https://www.sagelib.com";
String apiKey = "***";
String url = "https://www.sagelib.com/chat/completions";
String jsonRequestBody = """
{
"model": "claude-fable-5-1",
"max_completion_tokens": 2048,
"messages": [
{
"role": "user",
"content": "What model are you? Which company made you? Briefly describe your capability boundaries."
}
]
}
""";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.header("Authorization", "Bearer " + apiKey)
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(jsonRequestBody))
.build();
try {
// Use ofInputStream to handle the streaming response
HttpResponse<java.io.InputStream> response = client.send(request, HttpResponse.BodyHandlers.ofInputStream());
try (BufferedReader reader = new BufferedReader(new InputStreamReader(response.body()))) {
String line;
while ((line = reader.readLine()) != null) {
line = line.trim();
if (line.startsWith("data:")) {
String data = line.substring(5).trim();
if ("[DONE]".equals(data)) {
break;
}
// Tip: you can bring in Gson or Jackson here to parse the JSON in data and extract delta.content
System.out.println("Received stream chunk: " + data);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
func main() {
baseUrl := "https://www.sagelib.com"
apiKey := "***"
url := "https://www.sagelib.com/chat/completions"
jsonRequestBody := `
{
"model": "claude-fable-5-1",
"max_completion_tokens": 2048,
"messages": [
{
"role": "user",
"content": "What model are you? Which company made you? Briefly describe your capability boundaries."
}
]
}
`
req, err := http.NewRequest("POST", url, bytes.NewBufferString(jsonRequestBody))
if err != nil {
fmt.Println("Failed to create request:", err)
return
}
req.Header.Set("Authorization", "Bearer "+apiKey)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Request failed:", err)
return
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
// Pretty-print the returned JSON data
var prettyBody map[string]interface{}
if err := json.Unmarshal(body, &prettyBody); err == nil {
pretty, _ := json.MarshalIndent(prettyBody, "", " ")
fmt.Println("Status Code:", resp.StatusCode)
fmt.Println(string(pretty))
} else {
fmt.Println("Status Code:", resp.StatusCode)
fmt.Println(string(body))
}
}
curl -v 'https://www.sagelib.com/chat/completions' \
-H "Authorization: Bearer ***" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-fable-5-1",
"max_completion_tokens": 4096,
"messages": [
{
"role": "user",
"content": "How is the weather in Beijing today? Please use a tool to query."
}
],
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Query real-time weather for a specified city",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string"
}
},
"required": [
"city"
]
}
}
}
]
}'
import json
import requests
# Configure base variables
base_url = "https://www.sagelib.com" # 把 base_url 换成你的网关地址 https://www.sagelib.com
api_key = "***" # Replace with your Bearer token
url = f"https://www.sagelib.com/chat/completions"
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json",
}
data = {
"model": "claude-fable-5-1",
"max_completion_tokens": 4096,
"messages": [
{
"role": "user",
"content": "How is the weather in Beijing today? Please use a tool to query."
}
],
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Query real-time weather for a specified city",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string"
}
},
"required": [
"city"
]
}
}
}
]
}
# Send the POST request
response = requests.post(url, headers=headers, json=data)
# Print the status code and the returned JSON data
print(f"Status Code: {response.status_code}")
try:
print(json.dumps(response.json(), indent=2, ensure_ascii=False))
except Exception:
print(response.text)
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class LlmStreamClient {
public static void main(String[] args) {
String baseUrl = "https://www.sagelib.com";
String apiKey = "***";
String url = "https://www.sagelib.com/chat/completions";
String jsonRequestBody = """
{
"model": "claude-fable-5-1",
"max_completion_tokens": 4096,
"messages": [
{
"role": "user",
"content": "How is the weather in Beijing today? Please use a tool to query."
}
],
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Query real-time weather for a specified city",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string"
}
},
"required": [
"city"
]
}
}
}
]
}
""";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.header("Authorization", "Bearer " + apiKey)
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(jsonRequestBody))
.build();
try {
// Use ofInputStream to handle the streaming response
HttpResponse<java.io.InputStream> response = client.send(request, HttpResponse.BodyHandlers.ofInputStream());
try (BufferedReader reader = new BufferedReader(new InputStreamReader(response.body()))) {
String line;
while ((line = reader.readLine()) != null) {
line = line.trim();
if (line.startsWith("data:")) {
String data = line.substring(5).trim();
if ("[DONE]".equals(data)) {
break;
}
// Tip: you can bring in Gson or Jackson here to parse the JSON in data and extract delta.content
System.out.println("Received stream chunk: " + data);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
func main() {
baseUrl := "https://www.sagelib.com"
apiKey := "***"
url := "https://www.sagelib.com/chat/completions"
jsonRequestBody := `
{
"model": "claude-fable-5-1",
"max_completion_tokens": 4096,
"messages": [
{
"role": "user",
"content": "How is the weather in Beijing today? Please use a tool to query."
}
],
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Query real-time weather for a specified city",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string"
}
},
"required": [
"city"
]
}
}
}
]
}
`
req, err := http.NewRequest("POST", url, bytes.NewBufferString(jsonRequestBody))
if err != nil {
fmt.Println("Failed to create request:", err)
return
}
req.Header.Set("Authorization", "Bearer "+apiKey)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Request failed:", err)
return
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
// Pretty-print the returned JSON data
var prettyBody map[string]interface{}
if err := json.Unmarshal(body, &prettyBody); err == nil {
pretty, _ := json.MarshalIndent(prettyBody, "", " ")
fmt.Println("Status Code:", resp.StatusCode)
fmt.Println(string(pretty))
} else {
fmt.Println("Status Code:", resp.StatusCode)
fmt.Println(string(body))
}
}
curl -v 'https://www.sagelib.com/chat/completions' \
-H "Authorization: Bearer ***" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-fable-5-1",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "Describe the main content of this image."
},
{
"type": "image_url",
"image_url": {
"url": "${image_url}"
}
}
]
}
]
}'
import json
import requests
# Configure base variables
base_url = "https://www.sagelib.com" # 把 base_url 换成你的网关地址 https://www.sagelib.com
api_key = "***" # Replace with your Bearer token
url = f"https://www.sagelib.com/chat/completions"
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json",
}
data = {
"model": "claude-fable-5-1",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "Describe the main content of this image."
},
{
"type": "image_url",
"image_url": {
"url": "${image_url}"
}
}
]
}
]
}
# Send the POST request
response = requests.post(url, headers=headers, json=data)
# Print the status code and the returned JSON data
print(f"Status Code: {response.status_code}")
try:
print(json.dumps(response.json(), indent=2, ensure_ascii=False))
except Exception:
print(response.text)
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class LlmStreamClient {
public static void main(String[] args) {
String baseUrl = "https://www.sagelib.com";
String apiKey = "***";
String url = "https://www.sagelib.com/chat/completions";
String jsonRequestBody = """
{
"model": "claude-fable-5-1",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "Describe the main content of this image."
},
{
"type": "image_url",
"image_url": {
"url": "${image_url}"
}
}
]
}
]
}
""";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.header("Authorization", "Bearer " + apiKey)
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(jsonRequestBody))
.build();
try {
// Use ofInputStream to handle the streaming response
HttpResponse<java.io.InputStream> response = client.send(request, HttpResponse.BodyHandlers.ofInputStream());
try (BufferedReader reader = new BufferedReader(new InputStreamReader(response.body()))) {
String line;
while ((line = reader.readLine()) != null) {
line = line.trim();
if (line.startsWith("data:")) {
String data = line.substring(5).trim();
if ("[DONE]".equals(data)) {
break;
}
// Tip: you can bring in Gson or Jackson here to parse the JSON in data and extract delta.content
System.out.println("Received stream chunk: " + data);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
func main() {
baseUrl := "https://www.sagelib.com"
apiKey := "***"
url := "https://www.sagelib.com/chat/completions"
jsonRequestBody := `
{
"model": "claude-fable-5-1",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "Describe the main content of this image."
},
{
"type": "image_url",
"image_url": {
"url": "${image_url}"
}
}
]
}
]
}
`
req, err := http.NewRequest("POST", url, bytes.NewBufferString(jsonRequestBody))
if err != nil {
fmt.Println("Failed to create request:", err)
return
}
req.Header.Set("Authorization", "Bearer "+apiKey)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Request failed:", err)
return
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
// Pretty-print the returned JSON data
var prettyBody map[string]interface{}
if err := json.Unmarshal(body, &prettyBody); err == nil {
pretty, _ := json.MarshalIndent(prettyBody, "", " ")
fmt.Println("Status Code:", resp.StatusCode)
fmt.Println(string(pretty))
} else {
fmt.Println("Status Code:", resp.StatusCode)
fmt.Println(string(body))
}
}
响应参数
Body 参数
| 参数名称 | 描述 |
|---|---|
id string |
响应唯一标识,如 chatcmpl-${completion_id} |
object string |
chat.completion(流式为 chat.completion.chunk) |
created integer |
Unix 时间戳 |
model string |
实际使用的模型名称 |
choices array |
|
usage object |
|
prompt_tokens integer |
输入 token 数 |
completion_tokens integer |
输出 token 数(含思考 token) |
total_tokens integer |
总 token 数 |
prompt_tokens_details object |
|
cached_tokens integer |
缓存命中 token 数 |
cache_write_tokens integer |
缓存写入 token 数 |
inference_geo string |
实际推理地理区域 |
POSThttps://www.sagelib.com/anthropic/v1/messages
claude-fable-5-1 模型的聊天生成接口,Anthropic直连,支持多轮上下文、流式输出、工具调用、结构化输出等。
接口鉴权方式请参考:API接入与鉴权
请求参数
请求头
| 参数名称 | 描述 |
|---|---|
*Content-Type string |
可选值:application/json application/json |
*anthropic-version string |
可选值:2023-06-01 Anthropic API 版本号,固定为 2023-06-01 |
*X-Api-Key string |
鉴权令牌,格式为 ${AIGATEWAY_TOKEN} |
Body 参数
| 参数名称 | 描述 |
|---|---|
*model string |
可选值:"claude-fable-5-1" 模型名称,须精确匹配 |
*messages array |
可选值:content 对话消息列表,user/assistant 交替;单请求最多 100000 条;末条 assistant 预填不支持 |
*max_tokens integer |
可选值:0 ~ 128000 生成 token 硬上限(含思考+回复文本);0 仅预热 prompt cache |
system string |
可选值:String or array of text blocks 系统提示词,Anthropic 顶层参数(非 messages 数组内) |
stop_sequences array |
可选值:String array 自定义停止序列,命中后 stop_reason=stop_sequence |
stream boolean |
默认值:false 可选值:true、false 是否启用 SSE 流式输出 |
tools array |
可选值:Array of tool definitions 工具定义列表,含 name/description/input_schema,支持 strict: true 严格 schema |
tool_choice object |
可选值:none 工具选择策略,本模型支持 auto(默认)与 none |
thinking object |
可选值:summarized 思考配置,本模型唯一模式 adaptive(思考始终开启,省略或显式均可),深度由 output_config.effort 控制 |
output_config object |
可选值:medium、high、xhigh 输出配置,effort 控制思考/输出深度,format 为结构化输出 |
cache_control object |
可选值:1h 顶层缓存控制,自动对最后一个可缓存块打标记;最小可缓存长度 512 tokens |
metadata object |
可选值:Contains user_id sub-field 请求元数据,user_id 为外部用户标识(uuid/hash,勿含身份信息) |
service_tier string |
可选值:auto、standard_only 服务层级:auto(优先级容量可用则用)/ standard_only(仅标准容量) |
container object |
可选值:Contains id and skills sub-fields 容器标识跨请求复用,skills 为预加载技能列表 |
inference_geo string |
可选值:String 推理地理区域,未指定时用工作区默认值 |
user_profile_id string |
可选值:String 用户档案 ID(代表组织外主体发起请求时用),需 user-profiles beta header |
请求示例
curl -v 'https://www.sagelib.com/anthropic/messages' \
-H "x-api-key: ***" \
-H "anthropic-version: 2023-06-01" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-fable-5-1",
"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": "What model are you? Which company made you? When were you launched? Briefly describe your capability boundaries."
}
]
}'
import json
import requests
# Configure base variables
base_url = "https://www.sagelib.com" # 把 base_url 换成你的网关地址 https://www.sagelib.com
api_key = "***" # Replace with your API key
url = f"https://www.sagelib.com/anthropic/messages"
headers = {
"x-api-key": api_key,
"anthropic-version": "2023-06-01",
"Content-Type": "application/json",
}
data = {
"model": "claude-fable-5-1",
"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": "What model are you? Which company made you? When were you launched? Briefly describe your capability boundaries."
}
]
}
# Send the POST request
response = requests.post(url, headers=headers, json=data)
# Print the status code and the returned JSON data
print(f"Status Code: {response.status_code}")
try:
print(json.dumps(response.json(), indent=2, ensure_ascii=False))
except Exception:
print(response.text)
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class LlmStreamClient {
public static void main(String[] args) {
String baseUrl = "https://www.sagelib.com";
String apiKey = "***";
String url = "https://www.sagelib.com/anthropic/messages";
String jsonRequestBody = """
{
"model": "claude-fable-5-1",
"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": "What model are you? Which company made you? When were you launched? Briefly describe your capability boundaries."
}
]
}
""";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.header("x-api-key", apiKey).header("anthropic-version", "2023-06-01")
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(jsonRequestBody))
.build();
try {
// Use ofInputStream to handle the streaming response
HttpResponse<java.io.InputStream> response = client.send(request, HttpResponse.BodyHandlers.ofInputStream());
try (BufferedReader reader = new BufferedReader(new InputStreamReader(response.body()))) {
String line;
while ((line = reader.readLine()) != null) {
line = line.trim();
if (line.startsWith("data:")) {
String data = line.substring(5).trim();
if ("[DONE]".equals(data)) {
break;
}
// Tip: you can bring in Gson or Jackson here to parse the JSON in data and extract delta.content
System.out.println("Received stream chunk: " + data);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
func main() {
baseUrl := "https://www.sagelib.com"
apiKey := "***"
url := "https://www.sagelib.com/anthropic/messages"
jsonRequestBody := `
{
"model": "claude-fable-5-1",
"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": "What model are you? Which company made you? When were you launched? Briefly describe your capability boundaries."
}
]
}
`
req, err := http.NewRequest("POST", url, bytes.NewBufferString(jsonRequestBody))
if err != nil {
fmt.Println("Failed to create request:", err)
return
}
req.Header.Set("x-api-key", apiKey)
req.Header.Set("anthropic-version", "2023-06-01")
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Request failed:", err)
return
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
// Pretty-print the returned JSON data
var prettyBody map[string]interface{}
if err := json.Unmarshal(body, &prettyBody); err == nil {
pretty, _ := json.MarshalIndent(prettyBody, "", " ")
fmt.Println("Status Code:", resp.StatusCode)
fmt.Println(string(pretty))
} else {
fmt.Println("Status Code:", resp.StatusCode)
fmt.Println(string(body))
}
}
curl -v 'https://www.sagelib.com/anthropic/messages' \
-H "x-api-key: ***" \
-H "anthropic-version: 2023-06-01" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-fable-5-1",
"max_tokens": 1024,
"tools": [
{
"name": "get_weather",
"description": "Query the real-time weather of a specified city, including temperature, humidity, and wind",
"input_schema": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "City name, e.g. Beijing"
}
},
"required": [
"city"
]
}
}
],
"messages": [
{
"role": "user",
"content": "How is the weather in Beijing today? What is the temperature? Please call a tool to query and then answer."
}
]
}'
import json
import requests
# Configure base variables
base_url = "https://www.sagelib.com" # 把 base_url 换成你的网关地址 https://www.sagelib.com
api_key = "***" # Replace with your API key
url = f"https://www.sagelib.com/anthropic/messages"
headers = {
"x-api-key": api_key,
"anthropic-version": "2023-06-01",
"Content-Type": "application/json",
}
data = {
"model": "claude-fable-5-1",
"max_tokens": 1024,
"tools": [
{
"name": "get_weather",
"description": "Query the real-time weather of a specified city, including temperature, humidity, and wind",
"input_schema": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "City name, e.g. Beijing"
}
},
"required": [
"city"
]
}
}
],
"messages": [
{
"role": "user",
"content": "How is the weather in Beijing today? What is the temperature? Please call a tool to query and then answer."
}
]
}
# Send the POST request
response = requests.post(url, headers=headers, json=data)
# Print the status code and the returned JSON data
print(f"Status Code: {response.status_code}")
try:
print(json.dumps(response.json(), indent=2, ensure_ascii=False))
except Exception:
print(response.text)
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class LlmStreamClient {
public static void main(String[] args) {
String baseUrl = "https://www.sagelib.com";
String apiKey = "***";
String url = "https://www.sagelib.com/anthropic/messages";
String jsonRequestBody = """
{
"model": "claude-fable-5-1",
"max_tokens": 1024,
"tools": [
{
"name": "get_weather",
"description": "Query the real-time weather of a specified city, including temperature, humidity, and wind",
"input_schema": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "City name, e.g. Beijing"
}
},
"required": [
"city"
]
}
}
],
"messages": [
{
"role": "user",
"content": "How is the weather in Beijing today? What is the temperature? Please call a tool to query and then answer."
}
]
}
""";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.header("x-api-key", apiKey).header("anthropic-version", "2023-06-01")
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(jsonRequestBody))
.build();
try {
// Use ofInputStream to handle the streaming response
HttpResponse<java.io.InputStream> response = client.send(request, HttpResponse.BodyHandlers.ofInputStream());
try (BufferedReader reader = new BufferedReader(new InputStreamReader(response.body()))) {
String line;
while ((line = reader.readLine()) != null) {
line = line.trim();
if (line.startsWith("data:")) {
String data = line.substring(5).trim();
if ("[DONE]".equals(data)) {
break;
}
// Tip: you can bring in Gson or Jackson here to parse the JSON in data and extract delta.content
System.out.println("Received stream chunk: " + data);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
func main() {
baseUrl := "https://www.sagelib.com"
apiKey := "***"
url := "https://www.sagelib.com/anthropic/messages"
jsonRequestBody := `
{
"model": "claude-fable-5-1",
"max_tokens": 1024,
"tools": [
{
"name": "get_weather",
"description": "Query the real-time weather of a specified city, including temperature, humidity, and wind",
"input_schema": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "City name, e.g. Beijing"
}
},
"required": [
"city"
]
}
}
],
"messages": [
{
"role": "user",
"content": "How is the weather in Beijing today? What is the temperature? Please call a tool to query and then answer."
}
]
}
`
req, err := http.NewRequest("POST", url, bytes.NewBufferString(jsonRequestBody))
if err != nil {
fmt.Println("Failed to create request:", err)
return
}
req.Header.Set("x-api-key", apiKey)
req.Header.Set("anthropic-version", "2023-06-01")
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Request failed:", err)
return
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
// Pretty-print the returned JSON data
var prettyBody map[string]interface{}
if err := json.Unmarshal(body, &prettyBody); err == nil {
pretty, _ := json.MarshalIndent(prettyBody, "", " ")
fmt.Println("Status Code:", resp.StatusCode)
fmt.Println(string(pretty))
} else {
fmt.Println("Status Code:", resp.StatusCode)
fmt.Println(string(body))
}
}
curl -v 'https://www.sagelib.com/anthropic/messages' \
-H "x-api-key: ***" \
-H "anthropic-version: 2023-06-01" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-fable-5-1",
"max_tokens": 2048,
"system": "[Enterprise KB v3.2] Place a long system prompt text here (e.g. enterprise KB, business rules); length over 512 tokens triggers cache write.",
"cache_control": {
"type": "ephemeral"
},
"messages": [
{
"role": "user",
"content": "Carefully read the long text above and summarize the key points; extract 3-5 conclusions explaining the relationship between the caching mechanism and billing rules."
}
]
}'
import json
import requests
# Configure base variables
base_url = "https://www.sagelib.com" # 把 base_url 换成你的网关地址 https://www.sagelib.com
api_key = "***" # Replace with your API key
url = f"https://www.sagelib.com/anthropic/messages"
headers = {
"x-api-key": api_key,
"anthropic-version": "2023-06-01",
"Content-Type": "application/json",
}
data = {
"model": "claude-fable-5-1",
"max_tokens": 2048,
"system": "[Enterprise KB v3.2] Place a long system prompt text here (e.g. enterprise KB, business rules); length over 512 tokens triggers cache write.",
"cache_control": {
"type": "ephemeral"
},
"messages": [
{
"role": "user",
"content": "Carefully read the long text above and summarize the key points; extract 3-5 conclusions explaining the relationship between the caching mechanism and billing rules."
}
]
}
# Send the POST request
response = requests.post(url, headers=headers, json=data)
# Print the status code and the returned JSON data
print(f"Status Code: {response.status_code}")
try:
print(json.dumps(response.json(), indent=2, ensure_ascii=False))
except Exception:
print(response.text)
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class LlmStreamClient {
public static void main(String[] args) {
String baseUrl = "https://www.sagelib.com";
String apiKey = "***";
String url = "https://www.sagelib.com/anthropic/messages";
String jsonRequestBody = """
{
"model": "claude-fable-5-1",
"max_tokens": 2048,
"system": "[Enterprise KB v3.2] Place a long system prompt text here (e.g. enterprise KB, business rules); length over 512 tokens triggers cache write.",
"cache_control": {
"type": "ephemeral"
},
"messages": [
{
"role": "user",
"content": "Carefully read the long text above and summarize the key points; extract 3-5 conclusions explaining the relationship between the caching mechanism and billing rules."
}
]
}
""";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.header("x-api-key", apiKey).header("anthropic-version", "2023-06-01")
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(jsonRequestBody))
.build();
try {
// Use ofInputStream to handle the streaming response
HttpResponse<java.io.InputStream> response = client.send(request, HttpResponse.BodyHandlers.ofInputStream());
try (BufferedReader reader = new BufferedReader(new InputStreamReader(response.body()))) {
String line;
while ((line = reader.readLine()) != null) {
line = line.trim();
if (line.startsWith("data:")) {
String data = line.substring(5).trim();
if ("[DONE]".equals(data)) {
break;
}
// Tip: you can bring in Gson or Jackson here to parse the JSON in data and extract delta.content
System.out.println("Received stream chunk: " + data);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
func main() {
baseUrl := "https://www.sagelib.com"
apiKey := "***"
url := "https://www.sagelib.com/anthropic/messages"
jsonRequestBody := `
{
"model": "claude-fable-5-1",
"max_tokens": 2048,
"system": "[Enterprise KB v3.2] Place a long system prompt text here (e.g. enterprise KB, business rules); length over 512 tokens triggers cache write.",
"cache_control": {
"type": "ephemeral"
},
"messages": [
{
"role": "user",
"content": "Carefully read the long text above and summarize the key points; extract 3-5 conclusions explaining the relationship between the caching mechanism and billing rules."
}
]
}
`
req, err := http.NewRequest("POST", url, bytes.NewBufferString(jsonRequestBody))
if err != nil {
fmt.Println("Failed to create request:", err)
return
}
req.Header.Set("x-api-key", apiKey)
req.Header.Set("anthropic-version", "2023-06-01")
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Request failed:", err)
return
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
// Pretty-print the returned JSON data
var prettyBody map[string]interface{}
if err := json.Unmarshal(body, &prettyBody); err == nil {
pretty, _ := json.MarshalIndent(prettyBody, "", " ")
fmt.Println("Status Code:", resp.StatusCode)
fmt.Println(string(pretty))
} else {
fmt.Println("Status Code:", resp.StatusCode)
fmt.Println(string(body))
}
}
响应参数
Body 参数
| 参数名称 | 描述 |
|---|---|
id string |
响应唯一标识,如 msg_${message_id} |
type string |
固定为 message |
role string |
固定为 assistant |
model string |
实际使用的模型名称 |
content array |
内容块数组 |
stop_reason string |
停止原因:end_turn / stop_sequence / tool_use |
stop_sequence string |
匹配的停止序列(stop_reason=stop_sequence 时) |
usage object |
|
input_tokens integer |
输入 token 数 |
output_tokens integer |
输出 token 数(含思考 token) |
output_tokens_details object |
|
thinking_tokens integer |
思考 token 数 |
cache_creation object |
|
ephemeral_5m_input_tokens integer |
5 分钟 TTL 缓存写入 token 数 |
ephemeral_1h_input_tokens integer |
1 小时 TTL 缓存写入 token 数 |
cache_read_input_tokens integer |
缓存命中 token 数 |
inference_geo string |
实际推理地理区域 |