Skip to content

Claude API Examples

This page provides examples of using the Agentsflare Claude API to help you quickly integrate and use Claude AI services.

Basic Configuration

Before starting to use the API, please ensure you have obtained an API Key. If not, please refer to Create API Key.

Basic Information

  • API Base URL: https://api.agentsflare.com/anthropic/v1/messages
  • Authentication Method: Bearer Token (x-api-key header)
  • Content Type: application/json

Request Examples

bash
curl --location --request POST 'https://api.agentsflare.com/anthropic/v1/messages' \
--header 'Content-Type: application/json' \
--header 'x-api-key: YOUR_API_KEY' \
--header 'anthropic-version: 2023-06-01' \
--data-raw '{
    "model": "claude-sonnet-4-5-20250929",
    "max_tokens": 1024,
    "messages": [
        {
            "role": "user",
            "content": "Hello, please introduce yourself"
        }
    ]
}'
python
import anthropic

client = anthropic.Anthropic(
    api_key="YOUR_API_KEY",
    base_url="https://api.agentsflare.com/anthropic"
)

message = client.messages.create(
    model="claude-sonnet-4-5-20250929",
    max_tokens=1024,
    messages=[
        {
            "role": "user",
            "content": "Hello, please introduce yourself"
        }
    ]
)

print(message.content[0].text)
python
import anthropic

client = anthropic.Anthropic(
    api_key="YOUR_API_KEY",
    base_url="https://api.agentsflare.com/anthropic"
)

message = client.messages.create(
    model="claude-sonnet-4-5-20250929",
    max_tokens=1024,
    messages=[
        {
            "role": "user",
            "content": "Hello, please introduce yourself"
        }
    ],
    stream=True
)

for event in message:
    if event.type == "content_block_delta":
        delta = event.delta
        if delta.type == "text_delta":
            print(delta.text, end="", flush=True)
javascript
import Anthropic from "@anthropic-ai/sdk";

const client = new Anthropic({
  apiKey: process.env.AGENTSFLARE_API_KEY,
  baseURL: "https://api.agentsflare.com/anthropic"
});

async function main() {
  try {
    const message = await client.messages.create({
      model: "claude-sonnet-4-5-20250929",
      max_tokens: 1024,
      messages: [
        {
          role: "user",
          content: "Hello, please introduce yourself"
        }
      ]
    });

    console.log(message.content[0].text);
  } catch (err) {
    console.error(err?.response?.data ?? err);
  }
}

main();
javascript
import Anthropic from "@anthropic-ai/sdk";

const client = new Anthropic({
  apiKey: process.env.AGENTSFLARE_API_KEY,
  baseURL: "https://api.agentsflare.com/anthropic"
});

async function main() {
  try {
    const stream = await client.messages.create({
      model: "claude-sonnet-4-5-20250929",
      max_tokens: 1024,
      messages: [
        {
          role: "user",
          content: "Hello, please introduce yourself"
        }
      ],
      stream: true
    });

    for await (const event of stream) {
      if (event.type === 'content_block_delta') {
        if (event.delta.type === 'text_delta') {
          process.stdout.write(event.delta.text);
        }
      }
    }
  } catch (err) {
    console.error(err?.response?.data ?? err);
  }
}

main();
javascript
const Anthropic = require("@anthropic-ai/sdk");

const client = new Anthropic({
  apiKey: process.env.AGENTSFLARE_API_KEY,
  baseURL: "https://api.agentsflare.com/anthropic"
});

async function main() {
  try {
    const message = await client.messages.create({
      model: "claude-sonnet-4-5-20250929",
      max_tokens: 1024,
      messages: [
        {
          role: "user",
          content: "Hello, please introduce yourself"
        }
      ]
    });

    console.log(message.content[0].text);
  } catch (err) {
    console.error(err?.response?.data ?? err);
  }
}

main();
go
package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"github.com/anthropics/anthropic-sdk-go"
	"github.com/anthropics/anthropic-sdk-go/option"
)

func main() {
	apiKey := os.Getenv("AGENTSFLARE_API_KEY")
	if apiKey == "" {
		log.Fatal("missing env AGENTSFLARE_API_KEY")
	}

	client := anthropic.NewClient(
		option.WithAPIKey(apiKey),
		option.WithBaseURL("https://api.agentsflare.com/anthropic"),
	)

	ctx := context.Background()

	message, err := client.Messages.New(ctx, anthropic.MessageNewParams{
		Model:     anthropic.F("claude-sonnet-4-5-20250929"),
		MaxTokens: anthropic.F(int64(1024)),
		Messages: anthropic.F([]anthropic.MessageParam{
			anthropic.NewUserMessage(anthropic.NewTextBlock("Hello, please introduce yourself")),
		}),
	})

	if err != nil {
		log.Fatalf("message creation failed: %v", err)
	}

	fmt.Println(message.Content[0].Text)
}

Response Examples

Non-streaming Response

json
{
  "id": "msg_01XFDUDYJgAACzvnptvVoYEL",
  "type": "message",
  "role": "assistant",
  "content": [
    {
      "type": "text",
      "text": "Hello! I'm Claude, an AI assistant created by Anthropic. I'm designed to be helpful, harmless, and honest. I can assist you with a wide variety of tasks including writing, analysis, programming, learning, and more. How can I help you today?"
    }
  ],
  "model": "claude-sonnet-4-5-20250929",
  "stop_reason": "end_turn",
  "stop_sequence": null,
  "usage": {
    "input_tokens": 15,
    "output_tokens": 65
  }
}

Streaming Response

json
event: message_start
data: {"type":"message_start","message":{"id":"msg_123","type":"message","role":"assistant","content":[],"model":"claude-sonnet-4-5-20250929","stop_reason":null,"usage":{"input_tokens":15,"output_tokens":0}}}

event: content_block_start
data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}

event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Hello"}}

event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"!"}}

event: content_block_stop
data: {"type":"content_block_stop","index":0}

event: message_delta
data: {"type":"message_delta","delta":{"stop_reason":"end_turn","stop_sequence":null},"usage":{"output_tokens":65}}

event: message_stop
data: {"type":"message_stop"}

Request Parameters

ParameterTypeRequiredDescription
modelstringYesModel name, e.g., claude-sonnet-4-5-20250929
messagesarrayYesArray of messages with role and content
max_tokensintegerYesMaximum tokens to generate
streambooleanNoEnable streaming response, default false
temperaturefloatNoSampling temperature, range 0-1, default 1
top_pfloatNoNucleus sampling parameter, default 1
top_kintegerNoTop-k sampling parameter
systemstringNoSystem prompt

Features

Streaming Output

Claude API supports streaming output (SSE) by setting stream: true. Streaming responses allow real-time content generation, providing a better user experience.

System Prompts

Claude supports system prompts via the system parameter to define the assistant's role and behavior.

Prompt Caching

Claude API supports Prompt Caching, allowing you to cache large context blocks in system or messages (such as project specifications, reference documents, etc.). Subsequent requests can reuse the cached content, significantly reducing token consumption and response latency.

Limitations

ItemLimit
Minimum cacheable tokens1024 tokens (content below 1024 tokens will not be cached)
Cache TTL per entry300 seconds (5 minutes), automatically refreshed on each cache hit

Usage

Use the array format for system and add the cache_control field to the content block you want to cache.

Recommendation

Add a project name identifier at the beginning of the cached text (e.g., [Project: project-alpha]) to effectively isolate cache resources between different projects and prevent cache content from being mixed.

bash
curl -s https://api.agentsflare.com/anthropic/v1/messages \
  -H "content-type: application/json" \
  -H "x-api-key: YOUR_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -d '{
    "model": "claude-sonnet-4-5-20250929",
    "max_tokens": 1024,
    "system": [
      {
        "type": "text",
        "text": "[Project: project-alpha]\n\nPlace your large context here, such as project specs, technical docs, coding standards...",
        "cache_control": {"type": "ephemeral"}
      }
    ],
    "messages": [
      {
        "role": "user",
        "content": "Based on the above specs, help me review this code."
      }
    ]
  }'

Response Examples

First request (cache creation):

json
{
  "usage": {
    "input_tokens": 29,
    "cache_creation_input_tokens": 2480,  
    "cache_read_input_tokens": 0,         
    "output_tokens": 655
  }
}

Subsequent request (cache hit):

json
{
  "usage": {
    "input_tokens": 33,
    "cache_creation_input_tokens": 0,     
    "cache_read_input_tokens": 2480,      
    "output_tokens": 1024
  }
}

Response Field Descriptions

FieldDescription
cache_creation_input_tokensTokens newly cached in this request. A value greater than 0 indicates cache was created successfully
cache_read_input_tokensTokens read from cache in this request. A value greater than 0 indicates a successful cache hit
input_tokensRegular (non-cached) input tokens, such as user messages in messages

Determining Cache Status

  • cache_creation_input_tokens > 0: Cache created successfully — the content has been cached and subsequent identical requests will hit the cache
  • cache_read_input_tokens > 0: Cache hit successfully — this request reused previously cached content, saving token consumption
  • Both are 0: Cache did not take effect — the content may be below 1024 tokens or the cache has expired
python
message = client.messages.create(
    model="claude-sonnet-4-5-20250929",
    max_tokens=1024,
    system="You are a professional programming assistant, skilled in Python and JavaScript",
    messages=[
        {"role": "user", "content": "How to implement quicksort?"}
    ]
)

Multi-turn Conversations

Claude API supports multi-turn conversations by including history in the messages array:

python
messages = [
    {"role": "user", "content": "What is machine learning?"},
    {"role": "assistant", "content": "Machine learning is a branch of AI..."},
    {"role": "user", "content": "What are its applications?"}
]

message = client.messages.create(
    model="claude-sonnet-4-5-20250929",
    max_tokens=1024,
    messages=messages
)

Prompt Caching

Prompt Caching allows you to cache frequently reused content (such as long system prompts or document excerpts) on the API side. Subsequent requests that hit the cache will have significantly lower latency and cost. Add anthropic-beta: prompt-caching-2024-07-31 to the request headers and include "cache_control": {"type": "ephemeral"} on the content blocks you want to cache.

bash
curl --location --request POST 'https://api.agentsflare.com/anthropic/v1/messages' \
--header 'Content-Type: application/json' \
--header 'x-api-key: YOUR_API_KEY' \
--header 'anthropic-version: 2023-06-01' \
--header 'anthropic-beta: prompt-caching-2024-07-31' \
--data-raw '{
    "model": "claude-sonnet-4-5-20250929",
    "max_tokens": 1024,
    "system": [
        {
            "type": "text",
            "text": "You are a professional legal advisor. The following is the full contract text for your reference and analysis:\n\n[Large contract content here, can be thousands of words...]",
            "cache_control": {"type": "ephemeral"}
        }
    ],
    "messages": [
        {
            "role": "user",
            "content": "Please analyze the breach of contract clauses"
        }
    ]
}'
python
import anthropic

client = anthropic.Anthropic(
    api_key="YOUR_API_KEY",
    base_url="https://api.agentsflare.com/anthropic"
)

response = client.beta.prompt_caching.messages.create(
    model="claude-sonnet-4-5-20250929",
    max_tokens=1024,
    system=[
        {
            "type": "text",
            "text": "You are a professional legal advisor. The following is the full contract text for your reference and analysis:\n\n[Large contract content here, can be thousands of words...]",
            "cache_control": {"type": "ephemeral"}
        }
    ],
    messages=[
        {
            "role": "user",
            "content": "Please analyze the breach of contract clauses"
        }
    ]
)

print(response.content[0].text)
# Check cache usage
print(f"Cache creation tokens: {response.usage.cache_creation_input_tokens}")
print(f"Cache read tokens: {response.usage.cache_read_input_tokens}")
javascript
import Anthropic from "@anthropic-ai/sdk";

const client = new Anthropic({
  apiKey: process.env.AGENTSFLARE_API_KEY,
  baseURL: "https://api.agentsflare.com/anthropic"
});

async function main() {
  const response = await client.beta.promptCaching.messages.create({
    model: "claude-sonnet-4-5-20250929",
    max_tokens: 1024,
    system: [
      {
        type: "text",
        text: "You are a professional legal advisor. The following is the full contract text for your reference and analysis:\n\n[Large contract content here, can be thousands of words...]",
        cache_control: { type: "ephemeral" }
      }
    ],
    messages: [
      {
        role: "user",
        content: "Please analyze the breach of contract clauses"
      }
    ]
  });

  console.log(response.content[0].text);
  // Check cache usage
  console.log(`Cache creation tokens: ${response.usage.cache_creation_input_tokens}`);
  console.log(`Cache read tokens: ${response.usage.cache_read_input_tokens}`);
}

main();

Tip: Cached content remains valid for 5 minutes. The first request creates the cache (cache_creation_input_tokens); subsequent cache hits (cache_read_input_tokens) cost approximately 10% of the original input price.

Important Notes

  1. API Key Security: Do not hardcode API Keys in your code, use environment variables
  2. Request Rate: Please comply with API call rate limits
  3. Error Handling: Implement comprehensive error handling mechanisms
  4. Token Limits: Be aware of different models' context window limits

## Request Parameters

Parameters see [Chat Completions API](https://platform.openai.com/docs/api-reference/chat/create)

This documentation is licensed under CC BY-SA 4.0.