Anthropic provides a compatibility layer that allows you to use the OpenAI SDK to interact with Claude models.

This is useful for quickly evaluating Anthropic’s capabilities with minimal code changes.

TypeScript

import { OpenAI } from 'openai';
import { AgentRPC } from 'agentrpc';

const openai = new OpenAI({
  apiKey: process.env.ANTHROPIC_API_KEY,
  baseURL: 'https://api.anthropic.com/v1/',
});
const rpc = new AgentRPC({ apiSecret: process.env.AGENTRPC_API_SECRET });

const main = async () => {
  const tools = await rpc.OpenAI.getTools();
  const completion = await openai.chat.completions.create({
    model: 'claude-3-sonnet-20240229',
    messages: [
      {
        role: 'user',
        content: 'What is the weather in Melbourne?',
      },
    ],
    tools,
  });

  const message = completion.choices[0]?.message;

  if (message?.tool_calls) {
    for (const toolCall of message.tool_calls) {
      console.log('Agent is calling Tool', toolCall.function.name);
      const result = await rpc.OpenAI.executeTool(toolCall);
      console.log(result);
    }
  }
};

main();

Python

import os

from agentrpc import AgentRPC
from openai import OpenAI


def main():
    agentrpc = AgentRPC(api_secret=os.environ.get("AGENTRPC_API_SECRET", ""))
    openai = OpenAI(
        api_key=os.environ.get("ANTHROPIC_API_KEY", ""),
        base_url="https://api.anthropic.com/v1/",
    )

    tools = agentrpc.openai.completions.get_tools()

    completion = openai.chat.completions.create(
        model="claude-3-sonnet-20240229",
        messages=[{"role": "user", "content": "What is the weather in Melbourne?"}],
        tools=tools,
    )

    if completion.choices[0].message.tool_calls:
        for tool_call in completion.choices[0].message.tool_calls:
            print("Agent is calling Tool", tool_call.function.name)
            result = agentrpc.openai.completions.execute_tool(tool_call)
            print(result)


if __name__ == "__main__":
    main()