Browse by type
The AI SDK CPP is a modern C++ toolkit designed to help you build AI-powered applications with popular model providers like OpenAI and Anthropic. It provides a unified, easy-to-use API that abstracts away the complexity of different provider implementations.
C++ developers have long lacked a first-class, convenient way to interact with modern AI services like OpenAI, Anthropic, and others. AI SDK CPP bridges this gap by providing:
You will need a C++20 compatible compiler and CMake 3.16+ installed on your development machine.
The AI SDK CPP Core module provides a unified API to interact with model providers like OpenAI and Anthropic.
#include <ai/openai.h>
#include <ai/core.h>
#include <iostream>
int main() {
// Ensure OPENAI_API_KEY environment variable is set
auto client = ai::openai::create_client();
ai::GenerateOptions options(ai::openai::models::kGpt56,
"Why is the sky blue?");
options.system = "You are a friendly assistant!";
auto result = client.generate_text(options);
if (result) {
std::cout << result->text << std::endl;
}
return 0;
}
#include <ai/anthropic.h>
#include <ai/core.h>
#include <iostream>
int main() {
// Ensure ANTHROPIC_API_KEY environment variable is set
auto client = ai::anthropic::create_client();
ai::GenerateOptions options(ai::anthropic::models::kClaudeSonnet5,
"Explain quantum computing in simple terms.");
options.system = "You are a helpful assistant.";
auto result = client.generate_text(options);
if (result) {
std::cout << result->text << std::endl;
}
return 0;
}
#include <ai/openai.h>
#include <ai/core.h>
#include <iostream>
int main() {
auto client = ai::openai::create_client();
ai::GenerateOptions generate_options(ai::openai::models::kGpt56,
"Write a short story about a robot.");
generate_options.system = "You are a helpful assistant.";
auto stream = client.stream_text(ai::StreamOptions(generate_options));
for (const auto& event : stream) {
if (event.is_text_delta()) {
std::cout << event.text_delta << std::flush;
}
}
return 0;
}
#include <ai/openai.h>
#include <ai/core.h>
#include <iostream>
int main() {
auto client = ai::openai::create_client();
ai::Messages messages = {
ai::Message::system("You are a helpful math tutor."),
ai::Message::user("What is 2 + 2?"),
ai::Message::assistant("2 + 2 equals 4."),
ai::Message::user("Now what is 4 + 4?")
};
auto result = client.generate_text(
ai::GenerateOptions(ai::openai::models::kGpt56, messages));
if (result) {
std::cout << result->text << std::endl;
}
return 0;
}
The AI SDK CPP supports function calling, allowing models to interact with external systems and APIs.
#include <ai/openai.h>
#include <ai/core.h>
#include <ai/tools.h>
#include <iostream>
// Define a tool function
ai::JsonValue get_weather(const ai::JsonValue& args, const ai::ToolExecutionContext& context) {
std::string location = args["location"].get<std::string>();
// Your weather API logic here
return ai::JsonValue{
{"location", location},
{"temperature", 72},
{"condition", "Sunny"}
};
}
int main() {
auto client = ai::openai::create_client();
// Create tools
ai::ToolSet tools = {
{"weather", ai::create_simple_tool(
"weather",
"Get current weather for a location",
{{"location", "string"}},
get_weather
)}
};
ai::GenerateOptions options(ai::openai::models::kGpt56,
"What's the weather like in San Francisco?");
options.tools = tools;
options.max_steps = 3;
auto result = client.generate_text(options);
if (result) {
std::cout << result->text << std::endl;
// Inspect tool calls and results
for (const auto& call : result->tool_calls) {
std::cout << "Tool: " << call.tool_name
<< ", Args: " << call.arguments.dump() << std::endl;
}
}
return 0;
}
For long-running operations, you can define asynchronous tools:
#include <future>
#include <thread>
#include <chrono>
// Async tool that returns a future
std::future<ai::JsonValue> fetch_data_async(const ai::JsonValue& args, const ai::ToolExecutionContext& context) {
return std::async(std::launch::async, [args]() {
// Simulate async operation
std::this_thread::sleep_for(std::chrono::seconds(1));
return ai::JsonValue{
{"data", "Fetched from API"},
{"timestamp", std::time(nullptr)}
};
});
}
int main() {
auto client = ai::openai::create_client();
ai::ToolSet tools = {
{"fetch_data", ai::create_simple_async_tool(
"fetch_data",
"Fetch data from external API",
{{"endpoint", "string"}},
fetch_data_async
)}
};
// Multiple async tools will execute in parallel
ai::GenerateOptions options(ai::openai::models::kGpt56,
"Fetch data from the user and product APIs");
options.tools = tools;
auto result = client.generate_text(options);
return 0;
}
Configure retry behavior for handling transient failures:
#include <ai/openai.h>
#include <ai/retry/retry_policy.h>
int main() {
// Configure custom retry behavior
ai::retry::RetryConfig retry_config;
retry_config.max_retries = 5; // More retries for unreliable networks
retry_config.initial_delay = std::chrono::milliseconds(1000);
retry_config.backoff_factor = 1.5; // Gentler backoff
// Create client with custom retry configuration
auto client = ai::openai::create_client(
"your-api-key",
"https://api.openai.com",
retry_config
);
// The client will automatically retry on transient failures:
// - Network errors
// - HTTP 408, 409, 429 (rate limits), and 5xx errors
auto result = client.generate_text(
ai::GenerateOptions(ai::openai::models::kGpt56, "Hello, world!"));
return 0;
}
The same retry-config overload is available from
ai::anthropic::create_client for Anthropic requests.
The OpenAI client can be used with any OpenAI-compatible API by specifying a custom base URL. This allows you to use alternative providers like OpenRouter, which offers access to multiple models through a unified API.
#include <ai/openai.h>
#include <ai/core.h>
#include <iostream>
#include <cstdlib>
int main() {
// Get API key from environment variable
const char* api_key = std::getenv("OPENROUTER_API_KEY");
if (!api_key) {
std::cerr << "Please set OPENROUTER_API_KEY environment variable\n";
return 1;
}
// Create client with OpenRouter's base URL
auto client = ai::openai::create_client(
api_key,
"https://openrouter.ai/api" // OpenRouter's OpenAI-compatible endpoint
);
// Use any model available on OpenRouter
ai::GenerateOptions options("anthropic/claude-sonnet-5",
"What are the benefits of using OpenRouter?");
options.system = "You are a helpful assistant.";
auto result = client.generate_text(options);
if (result) {
std::cout << result->text << std::endl;
}
return 0;
}
This approach works with any OpenAI-compatible API provider. Simply provide: 1. Your provider's API key 2. The provider's base URL endpoint 3. Model names as specified by your provider
See the OpenRouter example for a complete demonstration.
Check out our examples directory for more comprehensive usage examples:
This project uses a patched version of nlohmann/json to remove the dependency on localeconv(), which is not thread-safe. The patch ensures:
localeconv() function, allowing downstream users to safely use the library in multi-threaded environments without worrying about locale-related race conditionsThis modification improves both safety and portability of the JSON library in concurrent applications.
Inspired by the excellent Vercel AI SDK for TypeScript/JavaScript developers.
$ claude mcp add ai-sdk-cpp \
-- python -m otcore.mcp_server <graph>