Migrating from OpenAI to Anthropic
If you have an existing codebase built around OpenAI's API (ChatGPT), you might want to migrate to Anthropic to take advantage of Claude 3's superior context window or coding abilities.
Fortunately, the transition is relatively straightforward. Both APIs use a similar messages array structure, but there are a few critical differences you must rewrite.
1. The SDK and Initialization
Obviously, you must swap the openai npm package for the @anthropic-ai/sdk package and use a different API key.
2. The System Prompt Location
This is the biggest structural change.
OpenAI: The system prompt is just the first message in the messages array.
// OpenAI
messages: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "Hello!" }
]
Anthropic: The system prompt is extracted entirely from the array and becomes a top-level parameter.
// Anthropic
system: "You are a helpful assistant.",
messages: [
{ role: "user", content: "Hello!" }
]
3. Tool Calling Syntax
Both platforms support Tool Calling, but their JSON schemas differ slightly.
OpenAI uses a wrapper called functions or tools, and requires a parameters object.
Anthropic uses a tools array, and requires an input_schema object.
When handling the response, OpenAI returns tool_calls in a separate property, whereas Anthropic returns tool_use as a standard block inside the content array.
4. Prompting Philosophy
This is the most important, non-code difference.
OpenAI models (like GPT-4) are heavily trained on Markdown formatting. Anthropic models (Claude) are heavily trained on XML tags.
If you migrate an OpenAI prompt that looks like this:
# Instructions
Do the task.
# Data
Here is the data.
It will work reasonably well in Claude, but it won't be optimal. You should rewrite it into Anthropic's native "language":
<instructions>
Do the task.
</instructions>
<data>
Here is the data.
</data>
Claude's reasoning abilities increase dramatically when you switch from Markdown-style prompting to XML-style prompting.