Using the Anthropic SDK
This guide shows you how to use the official Anthropic SDK with Liona, allowing you to integrate Claude AI capabilities while maintaining security and cost control. Liona works as a drop-in replacement for Anthropic’s API, requiring minimal changes to your existing code.
JavaScript/TypeScript Integration
The Anthropic JavaScript/TypeScript SDK is fully compatible with Liona. Follow these steps to integrate it into your application.
Install the Anthropic SDK
If you haven’t already, install the Anthropic SDK using npm, yarn, or pnpm:
npm install @anthropic-ai/sdk
# or
yarn add @anthropic-ai/sdk
# or
pnpm add @anthropic-ai/sdk
Initialize the Anthropic client
Initialize the Anthropic client using your Liona access key instead of your Anthropic API key:
import Anthropic from '@anthropic-ai/sdk';
const anthropic = new Anthropic({
apiKey: 'your_liona_access_key_here', // Your Liona access key
baseURL: 'https://api.liona.ai/v1/provider/anthropic',
});
Use the Anthropic SDK as normal
Now you can use the Anthropic SDK exactly as you would normally, with all the same methods and parameters:
async function generateWithClaude() {
const message = await anthropic.messages.create({
model: 'claude-3-opus-20240229',
max_tokens: 1000,
messages: [
{
role: 'user',
content: 'Explain the concept of neural networks in simple terms.',
},
],
});
console.log(message.content);
}
Client-side usage (browser)
One of Liona’s key benefits is enabling secure client-side usage of the Anthropic SDK. You can safely use your Liona access key directly in browser-based applications:
// In a React component or other client-side code
import Anthropic from '@anthropic-ai/sdk';
function ClaudeComponent() {
const [result, setResult] = useState('');
async function handleSubmit(userInput) {
// Safe to use in client-side code with Liona!
const anthropic = new Anthropic({
apiKey: 'your_liona_access_key_here', // Your Liona access key
baseURL: 'https://api.liona.ai/v1/provider/anthropic',
});
const message = await anthropic.messages.create({
model: 'claude-3-sonnet-20240229',
max_tokens: 1000,
messages: [
{
role: 'user',
content: userInput,
},
],
});
setResult(message.content[0].text);
}
// Component rendering...
}
Handle rate limits and policy errors
When using Liona, you should check for rate limit errors (HTTP 429) or policy limit exceeded messages:
async function callAnthropic() {
try {
const message = await anthropic.messages.create({
model: 'claude-3-sonnet-20240229',
max_tokens: 1000,
messages: [
{
role: 'user',
content: 'Hello, Claude!',
},
],
});
return message.content[0].text;
} catch (error) {
if (error.status === 429 || error.message.includes('policy limit exceeded')) {
console.log('Rate limit or policy limit reached. Please try again later.');
// Handle gracefully - perhaps show a user-friendly message
} else {
console.error('Error:', error);
}
}
}
Python Integration
The Anthropic Python SDK also works seamlessly with Liona.
Install the Anthropic Python package
If you haven’t already, install the Anthropic Python package:
pip install anthropic
# or
poetry add anthropic
Initialize the Anthropic client
Initialize the Anthropic client with your Liona access key:
from anthropic import Anthropic
anthropic = Anthropic(
api_key="your_liona_access_key_here",
base_url="https://api.liona.ai/v1/provider/anthropic",
)
Use the Anthropic client as normal
You can now use the Anthropic client normally:
message = anthropic.messages.create(
model="claude-3-opus-20240229",
max_tokens=1000,
messages=[
{
"role": "user",
"content": "Explain quantum entanglement simply."
}
]
)
print(message.content[0].text)
Handle rate limits and policy errors
Handle rate limits and policy errors in your Python applications:
import anthropic
from anthropic import Anthropic
client = Anthropic(
api_key="your_liona_access_key_here",
base_url="https://api.liona.ai/v1/provider/anthropic",
)
try:
message = client.messages.create(
model="claude-3-sonnet-20240229",
max_tokens=1000,
messages=[
{
"role": "user",
"content": "Hello, Claude!"
}
]
)
print(message.content[0].text)
except anthropic.APIError as e:
if e.status_code == 429 or "policy limit exceeded" in str(e).lower():
print("Rate limit or policy limit reached. Please try again later.")
else:
print(f"Error: {e}")
Additional Integration Options
Environment Variables
You can use environment variables to configure the Anthropic SDK:
# JavaScript
ANTHROPIC_API_KEY=your_liona_access_key_here
ANTHROPIC_BASE_URL=https://api.liona.ai/v1/provider/anthropic
# Python
ANTHROPIC_API_KEY=your_liona_access_key_here
ANTHROPIC_BASE_URL=https://api.liona.ai/v1/provider/anthropic
Using with Next.js
For Next.js applications using server components:
// In app/api/route.js or similar
import Anthropic from '@anthropic-ai/sdk';
import { NextResponse } from 'next/server';
export async function POST(request) {
const { prompt } = await request.json();
const anthropic = new Anthropic({
apiKey: process.env.LIONA_ACCESS_KEY,
baseURL: 'https://api.liona.ai/v1/provider/anthropic',
});
const message = await anthropic.messages.create({
model: 'claude-3-sonnet-20240229',
max_tokens: 1000,
messages: [
{
role: 'user',
content: prompt,
},
],
});
return NextResponse.json({ result: message.content[0].text });
}
Common Issues and Troubleshooting
Rate Limits and Policies
If you encounter rate limits or permission errors, check:
- The policy assigned to your user in Liona
- Your current usage against the set limits
- Whether the specific Claude model is allowed by your policy
You can check your usage and limits in the Liona dashboard under the “Usage” section.
Error Response Codes
Liona preserves Anthropic’s error structure while adding additional context:
- HTTP 429: Rate limit or policy limit exceeded
- HTTP 403: Unauthorized access to a specific model or feature
- HTTP 401: Invalid or expired access key
Next Steps
Now that you’ve integrated the Anthropic SDK with Liona, you might want to:
- Set up provider balancing to distribute requests across multiple keys
- Configure request caching to improve performance and reduce costs
- Enable request tracing for debugging and auditing