Skip to main content

Anthropic Claude Integration

Protect Claude workflows by scanning inputs with Koreshield and routing requests through the proxy.

Installation

npm install koreshield @anthropic-ai/sdk
pip install koreshield anthropic

Basic Integration (TypeScript)

import { Koreshield } from "koreshield";
import Anthropic from "@anthropic-ai/sdk";

const koreshield = new Koreshield({
apiKey: process.env.KORESHIELD_API_KEY
});

const anthropic = new Anthropic({
apiKey: process.env.ANTHROPIC_API_KEY
});

export async function secureClaude(userMessage: string) {
const scan = await koreshield.scanPrompt(userMessage);
if (!scan.isSafe) {
throw new Error(`Threat: ${scan.threatLevel}`);
}

const message = await anthropic.messages.create({
model: "claude-3-5-sonnet-20241022",
max_tokens: 1024,
messages: [{ role: "user", content: userMessage }]
});

return message.content[0].text;
}

Basic Integration (Python)

import os
from koreshield import KoreShieldClient
from anthropic import Anthropic

koreshield = KoreShieldClient(api_key=os.environ["KORESHIELD_API_KEY"])
anthropic = Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])

def secure_claude(user_message: str) -> str:
scan = koreshield.scan_prompt(user_message)
if not scan.is_safe:
raise Exception(f"Threat: {scan.threat_level}")

message = anthropic.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=1024,
messages=[{"role": "user", "content": user_message}]
)

return message.content[0].text

Proxy Mode

const response = await fetch("http://localhost:8000/v1/chat/completions", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({
model: "claude-3-5-sonnet-20241022",
messages: [{ role: "user", content: "Summarize the incident." }]
})
});
import requests

response = requests.post(
"http://localhost:8000/v1/chat/completions",
json={
"model": "claude-3-5-sonnet-20241022",
"messages": [{"role": "user", "content": "Summarize the incident."}]
}
)

Proxy Mode (Streaming)

const response = await fetch("http://localhost:8000/v1/chat/completions", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({
model: "claude-3-5-sonnet-20241022",
stream: true,
messages: [{ role: "user", content: "Draft a compliance summary." }]
})
});
response = requests.post(
"http://localhost:8000/v1/chat/completions",
json={
"model": "claude-3-5-sonnet-20241022",
"stream": True,
"messages": [{"role": "user", "content": "Draft a compliance summary."}]
},
stream=True
)

for line in response.iter_lines():
if line:
print(line.decode("utf-8"))

Streaming

const stream = await anthropic.messages.stream({
model: "claude-3-5-sonnet-20241022",
max_tokens: 1024,
messages: [{ role: "user", content: "Draft a security summary." }]
});

for await (const event of stream) {
if (event.type === "content_block_delta" && event.delta.type === "text_delta") {
process.stdout.write(event.delta.text);
}
}
with anthropic.messages.stream(
model="claude-3-5-sonnet-20241022",
max_tokens=1024,
messages=[{"role": "user", "content": "Draft a security summary."}]
) as stream:
for text in stream.text_stream:
print(text, end="")

Tool Use

const tools: Anthropic.Tool[] = [
{
name: "search_database",
description: "Search the database",
input_schema: {
type: "object",
properties: { query: { type: "string" } },
required: ["query"]
}
}
];

const message = await anthropic.messages.create({
model: "claude-3-5-sonnet-20241022",
max_tokens: 1024,
messages: [{ role: "user", content: "Search for user 123" }],
tools
});
tools = [
{
"name": "search_database",
"description": "Search the database",
"input_schema": {
"type": "object",
"properties": {"query": {"type": "string"}},
"required": ["query"]
}
}
]

message = anthropic.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=1024,
messages=[{"role": "user", "content": "Search for user 123"}],
tools=tools
)

System Prompts and Multi-Turn

{
"model": "claude-3-5-sonnet-20241022",
"messages": [
{"role": "system", "content": "You are a compliance assistant."},
{"role": "user", "content": "Summarize the incident."},
{"role": "assistant", "content": "Summary..."},
{"role": "user", "content": "List next steps."}
]
}
payload = {
"model": "claude-3-5-sonnet-20241022",
"messages": [
{"role": "system", "content": "You are a compliance assistant."},
{"role": "user", "content": "Summarize the incident."},
{"role": "assistant", "content": "Summary..."},
{"role": "user", "content": "List next steps."}
]
}

Error Handling

  • 403 indicates a blocked request due to policy enforcement.
  • 429 or 5xx typically indicates provider or rate-limit issues.

Security Controls

security:
sensitivity: high
default_action: block
features:
sanitization: true
detection: true
policy_enforcement: true

Next Steps