Skip to content
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ set(OPENAI_SOURCES
src/providers/openai/openai_response_parser.cpp
src/providers/openai/openai_stream.cpp
src/providers/openai/openai_factory.cpp
src/providers/openai/openai_model_traits.cpp
src/providers/openai/openai_responses_client.cpp
src/providers/openai/openai_responses_request_builder.cpp
src/providers/openai/openai_responses_response_parser.cpp
src/providers/openai/openai_responses_stream.cpp
)

set(ANTHROPIC_SOURCES
Expand Down
50 changes: 49 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ int main() {
int main() {
// Ensure ANTHROPIC_API_KEY environment variable is set
auto client = ai::anthropic::create_client();
ai::GenerateOptions options(ai::anthropic::models::kClaudeSonnet5,
ai::GenerateOptions options(ai::anthropic::models::kClaudeSonnet55,
"Explain quantum computing in simple terms.");
options.system = "You are a helpful assistant.";
auto result = client.generate_text(options);
Expand Down Expand Up @@ -175,6 +175,53 @@ int main() {
}
```

#### OpenAI Responses API

`ai::openai::create_client` speaks Chat Completions, which OpenAI-compatible
providers implement too. The GPT-6 models (`kGpt62Astra`, `kGpt62Sol`,
`kGpt61Terra`, `kGpt6Luna`) call tools through the Responses API
(`POST /v1/responses`) only, so use `ai::openai::create_responses_client` for
them. It takes the same arguments as `create_client` and the same
`GenerateOptions`; neither client falls back to the other API.

```cpp
#include <ai/openai.h>
#include <ai/tools.h>
#include <iostream>

// get_weather as in the Tool Calling example above

int main() {
auto client = ai::openai::create_responses_client();

ai::GenerateOptions options(ai::openai::models::kGpt62Sol,
"What's the weather like in San Francisco?");
options.tools = {
{"weather", ai::create_simple_tool(
"weather", "Get current weather for a location",
{{"location", "string"}}, get_weather)}};
options.max_steps = 3;

auto result = client.generate_text(options);
if (result) {
std::cout << result.text << std::endl;
}
return 0;
}
```

Requests are stateless (`store: false`), and the whole conversation is sent
with every request. When a reasoning model calls tools, the API requires its
`reasoning` items to be sent back with the tool results. They are returned in
`GenerateResult::response_messages` as `ai::ProviderContentPart`s, which the
multi-step loop (`max_steps > 1`) replays automatically, as do the next
requests when you append `response_messages` to your conversation. Other
providers ignore these parts. `system` becomes the `instructions` of the
request, `max_tokens` becomes `max_output_tokens` (it counts reasoning tokens
too), and `response_format` becomes `text.format`. The Responses API has no
`frequency_penalty`, `presence_penalty`, or `seed`; they are ignored with a
warning. See the [Responses API example](examples/openai_responses.cpp).

#### Async Tool Calling

For long-running operations, you can define asynchronous tools:
Expand Down Expand Up @@ -331,6 +378,7 @@ Check out our [examples directory](examples/) for more comprehensive usage examp
- [Retry Configuration](examples/retry_config_example.cpp)
- [Basic Tool Calling](examples/tool_calling_basic.cpp)
- [Multi-Step Tool Workflows](examples/tool_calling_multistep.cpp)
- [OpenAI Responses API](examples/openai_responses.cpp) - Tool calling with the GPT-6 models
- [Async Tool Execution](examples/tool_calling_async.cpp)
- [Search Team Documents with Loom](examples/loom_document_search.md)
- [OpenRouter Integration](examples/openrouter_example.cpp) - Using OpenAI-compatible APIs
Expand Down
3 changes: 3 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ add_ai_example(retry_config_example retry_config_example.cpp)
add_ai_example(test_openai test_openai.cpp)
add_ai_example(test_anthropic test_anthropic.cpp)

# OpenAI Responses API example (tool calling with the GPT-6 models)
add_ai_example(openai_responses openai_responses.cpp)

# OpenRouter example (OpenAI-compatible)
add_ai_example(openrouter_example openrouter_example.cpp)

Expand Down
2 changes: 1 addition & 1 deletion examples/basic_chat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ int main() {

auto client4 = ai::anthropic::create_client();
ai::GenerateOptions options4;
options4.model = ai::anthropic::models::kClaudeSonnet5;
options4.model = ai::anthropic::models::kClaudeSonnet55;
options4.prompt =
"Write a haiku about programming. Just the haiku, nothing else.";

Expand Down
5 changes: 3 additions & 2 deletions examples/components/all/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ int main() {
try {
auto anthropic_client = ai::anthropic::create_client();
std::cout << "✓ Anthropic client created successfully\n";
std::cout << "✓ Available models: " << ai::anthropic::models::kClaudeSonnet5
<< ", " << ai::anthropic::models::kClaudeHaiku45 << "\n";
std::cout << "✓ Available models: "
<< ai::anthropic::models::kClaudeSonnet55 << ", "
<< ai::anthropic::models::kClaudeHaiku55 << "\n";
} catch (const std::exception& e) {
std::cout << "✗ Anthropic client failed: " << e.what() << "\n";
}
Expand Down
109 changes: 109 additions & 0 deletions examples/openai_responses.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/**
* OpenAI Responses API Example - AI SDK C++
*
* The GPT-6 models call tools through the Responses API only. This example
* creates a Responses API client with `ai::openai::create_responses_client`
* and shows:
* - a multi-step tool loop on GPT-6.2 Sol (the reasoning items of each step
* are replayed automatically along with the tool results)
* - continuing the conversation from `response_messages`
* - streaming
*
* Usage:
* export OPENAI_API_KEY=your_key_here
* ./openai_responses
*/

#include <iostream>
#include <string>

#include <ai/openai.h>
#include <ai/tools.h>

namespace {

ai::JsonValue get_weather(const ai::JsonValue& args,
const ai::ToolExecutionContext& /*context*/) {
const auto location = args["location"].get<std::string>();
std::cout << " [tool] weather(" << location << ")\n";
return {{"location", location},
{"temperature", 68},
{"unit", "Fahrenheit"},
{"condition", "Light rain"}};
}

ai::JsonValue get_time(const ai::JsonValue& args,
const ai::ToolExecutionContext& /*context*/) {
const auto city = args["city"].get<std::string>();
std::cout << " [tool] local_time(" << city << ")\n";
return {{"city", city}, {"local_time", "16:20"}};
}

} // namespace

int main() {
auto client = ai::openai::try_create_responses_client();
if (!client) {
std::cerr << "Set OPENAI_API_KEY to run this example\n";
return 1;
}
std::cout << "Using " << client->config_info() << "\n\n";

// 1. Multi-step tool calling.
ai::GenerateOptions options(
ai::openai::models::kGpt62Sol,
"What is the weather in Seattle, and what time is it there? Should I "
"take an umbrella for a walk now?");
options.tools = {
{"weather",
ai::create_simple_tool("weather", "Get the current weather for a city",
{{"location", "string"}}, get_weather)},
{"local_time",
ai::create_simple_tool("local_time", "Get the local time in a city",
{{"city", "string"}}, get_time)}};
options.max_steps = 5;

auto result = client->generate_text(options);
if (!result) {
std::cerr << "Error: " << result.error_message() << "\n";
return 1;
}
std::cout << "Answer (" << result.steps.size() << " steps): " << result.text
<< "\n";
std::cout << "Tokens: " << result.usage.total_tokens << "\n\n";

// 2. Continue the conversation. `response_messages` holds the assistant
// turns, tool results, and reasoning items of the previous call.
ai::Messages conversation = {ai::Message::user(options.prompt)};
conversation.insert(conversation.end(), result.response_messages.begin(),
result.response_messages.end());
conversation.push_back(ai::Message::user("And what about in two hours?"));

ai::GenerateOptions follow_up(ai::openai::models::kGpt62Sol,
std::move(conversation));
follow_up.tools = options.tools;
follow_up.max_steps = 3;
auto follow_up_result = client->generate_text(follow_up);
if (!follow_up_result) {
std::cerr << "Error: " << follow_up_result.error_message() << "\n";
return 1;
}
std::cout << "Follow-up: " << follow_up_result.text << "\n\n";

// 3. Streaming.
std::cout << "Streaming: ";
auto stream = client->stream_text(ai::StreamOptions(ai::GenerateOptions(
ai::openai::models::kGpt6Luna, "Write a haiku about rain.")));
for (const auto& event : stream) {
if (event.is_text_delta()) {
std::cout << event.text_delta << std::flush;
} else if (event.is_error()) {
std::cerr << "\nStream error: " << event.error.value_or("") << "\n";
return 1;
} else if (event.is_finish()) {
break;
}
}
std::cout << "\n";
return 0;
}
4 changes: 2 additions & 2 deletions examples/test_anthropic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ int main() {
// Test simple generation
std::cout << "Testing Anthropic text generation...\n\n";

ai::GenerateOptions options(ai::anthropic::models::kClaudeHaiku45,
ai::GenerateOptions options(ai::anthropic::models::kClaudeHaiku55,
"You are a helpful assistant.",
"Why is the sky blue? Give a short answer.");

Expand All @@ -35,7 +35,7 @@ int main() {
std::cout << "\nTesting streaming...\n";

ai::GenerateOptions stream_opts(
ai::anthropic::models::kClaudeHaiku45,
ai::anthropic::models::kClaudeHaiku55,
"Count from 1 to 5 slowly and with each number say 'tick'");
ai::StreamOptions stream_options(stream_opts);

Expand Down
2 changes: 1 addition & 1 deletion examples/tool_calling_async.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ int main() {
start_time = std::chrono::high_resolution_clock::now();

ai::GenerateOptions anthropic_options;
anthropic_options.model = ai::anthropic::models::kClaudeSonnet5;
anthropic_options.model = ai::anthropic::models::kClaudeSonnet55;
anthropic_options.prompt = R"(
Please help me with these THREE tasks. You MUST use the tools to complete ALL of them:
1. Use the fetch_news tool to get tech news articles
Expand Down
14 changes: 13 additions & 1 deletion include/ai/anthropic.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ namespace anthropic {

namespace models {
/// Current Anthropic model identifiers
///
/// Claude Fable 5.1 and later, Claude Opus 5.5 and Claude Sonnet 5.5 reject a
/// forced tool choice (`ToolChoice::required()` and `ToolChoice::specific()`)
/// with HTTP 400; use `ToolChoice::auto_choice()` and name the tool in the
/// prompt instead.
constexpr const char* kClaudeFable52 = "claude-fable-5-2";
constexpr const char* kClaudeOpus55 = "claude-opus-5-5";
constexpr const char* kClaudeSonnet55 = "claude-sonnet-5-5";
constexpr const char* kClaudeHaiku55 = "claude-haiku-5-5";

/// Previous-generation model identifiers (still available)
constexpr const char* kClaudeFable51 = "claude-fable-5-1";
constexpr const char* kClaudeFable5 = "claude-fable-5";
constexpr const char* kClaudeOpus5 = "claude-opus-5";
constexpr const char* kClaudeOpus48 = "claude-opus-4-8";
Expand All @@ -39,7 +51,7 @@ constexpr const char* kClaudeOpus41 =
constexpr const char* kClaudeOpus41Snapshot = "claude-opus-4-1-20250805";

/// Default model used when none is specified
constexpr const char* kDefaultModel = kClaudeSonnet5;
constexpr const char* kDefaultModel = kClaudeSonnet55;
} // namespace models

/// Create an Anthropic client with default configuration
Expand Down
73 changes: 67 additions & 6 deletions include/ai/openai.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,48 @@ namespace openai {
namespace models {
/// Common OpenAI model identifiers

// GPT-5.6 series (current frontier family)
// GPT-6 series (current frontier family). These models reject sampling
// parameters, and OpenAI serves their tool calling through the Responses API
// only: use them with `create_responses_client`. On Chat Completions
// (`create_client`) they generate text but cannot call tools.
constexpr const char* kGpt62Astra = "gpt-6.2-astra";
constexpr const char* kGpt62Sol = "gpt-6.2-sol";
constexpr const char* kGpt61Terra = "gpt-6.1-terra";
constexpr const char* kGpt6Luna = "gpt-6-luna";

// GPT-5.6 series (latest family with tool calling on Chat Completions)
constexpr const char* kGpt56 = "gpt-5.6"; // Alias for GPT-5.6 Sol
constexpr const char* kGpt56Sol = "gpt-5.6-sol";
constexpr const char* kGpt56Terra = "gpt-5.6-terra";
constexpr const char* kGpt56Luna = "gpt-5.6-luna";

// Earlier current GPT-5 series
// Earlier GPT-5 series
constexpr const char* kGpt55 = "gpt-5.5";
constexpr const char* kGpt54 = "gpt-5.4";
constexpr const char* kGpt54Pro = "gpt-5.4-pro";
constexpr const char* kGpt54Mini = "gpt-5.4-mini";
constexpr const char* kGpt54Nano = "gpt-5.4-nano";
constexpr const char* kGpt54Nano = "gpt-5.4-nano"; // Shutdown: 2027-04-01

// GPT-5 small variants (current)
// GPT-5 small variants (deprecated, shutdown: 2026-12-11; use kGpt61Terra
// and kGpt6Luna)
constexpr const char* kGpt5Mini = "gpt-5-mini";
constexpr const char* kGpt5Nano = "gpt-5-nano";

// GPT-4.1 series (still current non-reasoning leaders)
// GPT-4.1 series (deprecated, shutdown: 2027-01-14; use kGpt6Luna)
constexpr const char* kGpt41 = "gpt-4.1";
constexpr const char* kGpt41Mini = "gpt-4.1-mini";

// Embeddings
constexpr const char* kTextEmbedding3Small = "text-embedding-3-small";
constexpr const char* kTextEmbedding3Large = "text-embedding-3-large";

/// Default model used when none is specified
/// Default model used when none is specified. It stays on GPT-5.6 because the
/// GPT-6 series does not support tool calling on Chat Completions.
constexpr const char* kDefaultModel = kGpt56;

/// Default model of the Responses API clients (`create_responses_client`).
constexpr const char* kDefaultResponsesModel = kGpt62Sol;

} // namespace models

/// Create an OpenAI client with default configuration
Expand Down Expand Up @@ -79,5 +93,52 @@ Client create_client(const std::string& api_key,
/// @note This is useful for chaining creation attempts with other providers
std::optional<Client> try_create_client();

/// The `create_responses_client` functions create OpenAI clients that use the
/// Responses API (`POST /v1/responses`) instead of Chat Completions. It is
/// required for tool calling with the GPT-6 models. The `create_client`
/// functions keep using Chat Completions, which is what OpenAI-compatible
/// providers (OpenRouter and others) implement; neither kind of client falls
/// back to the other API.
///
/// Requests are stateless (`store: false`): nothing is stored on OpenAI's
/// side, and the conversation is replayed on every request. The `reasoning`
/// items of a reply are returned in `GenerateResult::response_messages` as
/// `ProviderContentPart`s, and replayed when those messages are sent back,
/// which the multi-step tool loop (`max_steps > 1`) does automatically.
/// `frequency_penalty`, `presence_penalty`, and `seed` are not supported by
/// the Responses API and are ignored with a warning.

/// Create an OpenAI Responses API client
/// Reads API key from OPENAI_API_KEY environment variable
/// @return Configured OpenAI Responses API client
Client create_responses_client();

/// Create an OpenAI Responses API client with explicit API key
/// @param api_key OpenAI API key
/// @return Configured OpenAI Responses API client
Client create_responses_client(const std::string& api_key);

/// Create an OpenAI Responses API client with a custom base URL
/// @param api_key OpenAI API key
/// @param base_url Custom base URL (for Responses-compatible APIs)
/// @return Configured OpenAI Responses API client
Client create_responses_client(const std::string& api_key,
const std::string& base_url);

/// Create an OpenAI Responses API client with custom retry settings
/// @param api_key OpenAI API key
/// @param base_url Custom base URL (for Responses-compatible APIs)
/// @param retry_config Custom retry configuration
/// @return Configured OpenAI Responses API client
Client create_responses_client(const std::string& api_key,
const std::string& base_url,
const retry::RetryConfig& retry_config);

/// Try to create an OpenAI Responses API client using environment variables
/// Reads API key from OPENAI_API_KEY environment variable
/// @return Optional client - has value if environment variable is set, empty
/// otherwise
std::optional<Client> try_create_responses_client();

} // namespace openai
} // namespace ai
Loading
Loading