These C++ examples are real runnable files. Edit the source file first; this page is rebuilt from the checked-in example and its metadata header.
C++ Typed Generation
Runs a small typed generation program against OpenAI.
- Provider:
openai - Env:
OPENAI_API_KEY,OPENAI_APIKEY - Level:
beginner - Run:
npm run example -- cpp src/examples/cpp/generation/basic_generation.cpp - Source: src/examples/cpp/generation/basic_generation.cpp
#include "axllm/axllm.hpp"
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <sstream>
int main() {
const char* key = std::getenv("OPENAI_API_KEY");
if (key == nullptr || std::string(key).empty()) key = std::getenv("OPENAI_APIKEY");
if (key == nullptr || std::string(key).empty()) {
std::cerr << "Set OPENAI_API_KEY or OPENAI_APIKEY to run this example.\n";
return 2;
}
const char* model = std::getenv("AX_OPENAI_MODEL");
axllm::OpenAICompatibleClient client(axllm::object({
{"api_key", key},
{"model", model == nullptr || std::string(model).empty() ? "gpt-5.4-mini" : model},
{"model_config", axllm::object({{"temperature", 0}})},
}));
axllm::AxGen program = axllm::ax("question:string -> answer:string");
axllm::Value output = program.forward(client, axllm::object({{"question", "In one sentence, explain Ax as a language-agnostic LLM programming library."}}));
std::cout << axllm::stringify(output) << "\n";
}C++ Structured Extraction
Extracts structured fields and labels from support text with OpenAI.
- Provider:
openai - Env:
OPENAI_API_KEY,OPENAI_APIKEY - Level:
intermediate - Run:
npm run example -- cpp src/examples/cpp/generation/structured_generation.cpp - Source: src/examples/cpp/generation/structured_generation.cpp
#include "axllm/axllm.hpp"
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <sstream>
int main() {
const char* key = std::getenv("OPENAI_API_KEY");
if (key == nullptr || std::string(key).empty()) key = std::getenv("OPENAI_APIKEY");
if (key == nullptr || std::string(key).empty()) {
std::cerr << "Set OPENAI_API_KEY or OPENAI_APIKEY to run this example.\n";
return 2;
}
const char* model = std::getenv("AX_OPENAI_MODEL");
axllm::OpenAICompatibleClient client(axllm::object({
{"api_key", key},
{"model", model == nullptr || std::string(model).empty() ? "gpt-5.4-mini" : model},
{"model_config", axllm::object({{"temperature", 0}})},
}));
axllm::AxGen program = axllm::ax("ticket:string -> priority:class \"high, normal, low\", summary:string, labels:string[]");
axllm::Value output = program.forward(client, axllm::object({{"ticket", "Checkout has failed for enterprise customers since 09:00. Support wants a concise summary and tags."}}));
std::cout << axllm::stringify(output) << "\n";
}C++ Signature Constraints
Builds native constrained fields and runs the signature with OpenAI.
- Provider:
openai - Env:
OPENAI_API_KEY,OPENAI_APIKEY - Level:
intermediate - Run:
npm run example -- cpp src/examples/cpp/generation/signature-constraints.cpp - Source: src/examples/cpp/generation/signature-constraints.cpp
#include "axllm/axllm.hpp"
#include <cstdlib>
#include <iostream>
axllm::Value field(const char* name, const char* title, axllm::Value type) {
return axllm::Core::record_new(
"Field", axllm::object({{"name", name}, {"title", title}, {"type", type}}));
}
int main() {
const char* key = std::getenv("OPENAI_API_KEY");
if (key == nullptr || std::string(key).empty()) key = std::getenv("OPENAI_APIKEY");
if (key == nullptr || std::string(key).empty()) {
std::cerr << "Set OPENAI_API_KEY or OPENAI_APIKEY to run this example.\n";
return 2;
}
const char* model = std::getenv("AX_OPENAI_MODEL");
axllm::OpenAICompatibleClient client(axllm::object({
{"api_key", key},
{"model", model == nullptr || std::string(model).empty() ? "gpt-5.4-mini" : model},
{"model_config", axllm::object({{"temperature", 0}})},
}));
axllm::Value signature = axllm::Core::record_new(
"AxSignature",
axllm::object({
{"description", "Extract a constrained restaurant booking"},
{"inputs",
axllm::array({
field("requestText", "Request Text",
axllm::Core::record_new(
"FieldType",
axllm::object({{"name", "string"}, {"minLength", 10}, {"maxLength", 500}}))),
field("contactEmail", "Contact Email",
axllm::Core::record_new(
"FieldType", axllm::object({{"name", "string"}, {"format", "email"}}))),
})},
{"outputs",
axllm::array({
field("partySize", "Party Size",
axllm::Core::record_new(
"FieldType",
axllm::object({{"name", "number"}, {"minimum", 1}, {"maximum", 12}}))),
field("bookingCode", "Booking Code",
axllm::Core::record_new(
"FieldType",
axllm::object({
{"name", "string"},
{"pattern", "^[A-Z]{3}-\\d{4}$"},
{"patternDescription", "Must look like ABC-1234"},
}))),
})},
}));
axllm::Core::validate_signature(signature);
axllm::AxGen program = axllm::ax(signature);
axllm::Value output = program.forward(
client,
axllm::object({
{"requestText", "Book dinner for four people under the name Ada Lovelace."},
{"contactEmail", "ada@example.com"},
}));
std::cout << axllm::stringify(output) << "\n";
}Centralized Usage Observer
Attributes every completed model call to a tenant, user, and request from one global observer.
- Provider:
openai - Env:
OPENAI_API_KEY,OPENAI_APIKEY - Level:
intermediate - Run:
npm run example -- cpp src/examples/cpp/generation/usage_observer.cpp - Source: src/examples/cpp/generation/usage_observer.cpp
#include "axllm/axllm.hpp"
#include <chrono>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
int main() {
const char* api_key = std::getenv("OPENAI_API_KEY");
if (api_key == nullptr || std::string(api_key).empty()) api_key = std::getenv("OPENAI_APIKEY");
if (api_key == nullptr || std::string(api_key).empty()) {
std::cerr << "Set OPENAI_API_KEY or OPENAI_APIKEY to run this example.\n";
return 2;
}
const char* configured_model = std::getenv("AX_OPENAI_MODEL");
std::string model =
configured_model == nullptr || std::string(configured_model).empty()
? "gpt-5.4-mini"
: configured_model;
std::vector<axllm::AxUsageEvent> events;
axllm::set_usage_observer(
[&events](axllm::AxUsageEvent event) { events.push_back(std::move(event)); });
axllm::OpenAICompatibleClient client(axllm::object({
{"api_key", api_key},
{"model", model},
{"usageContext",
axllm::object({
{"tenantId", "tenant-42"},
{"feature", "support-chat"},
{"attributes", axllm::object({{"environment", "example"}})},
})},
}));
client.chat(
axllm::object({
{"chat_prompt",
axllm::array({
axllm::object({{"role", "user"}, {"content", "Reply with one short greeting."}}),
})},
}),
axllm::object({
{"usageContext",
axllm::object({
{"userId", "user-7"},
{"requestId",
"request-" +
std::to_string(
std::chrono::steady_clock::now().time_since_epoch().count())},
})},
}));
axllm::set_usage_observer({});
std::cout << axllm::stringify(axllm::Value(axllm::Array(events.begin(), events.end())))
<< "\n";
}C++ Contextual Generation
Answers from supplied context and returns compact citations with OpenAI.
- Provider:
openai - Env:
OPENAI_API_KEY,OPENAI_APIKEY - Level:
advanced - Run:
npm run example -- cpp src/examples/cpp/generation/context_generation.cpp - Source: src/examples/cpp/generation/context_generation.cpp
#include "axllm/axllm.hpp"
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <sstream>
int main() {
const char* key = std::getenv("OPENAI_API_KEY");
if (key == nullptr || std::string(key).empty()) key = std::getenv("OPENAI_APIKEY");
if (key == nullptr || std::string(key).empty()) {
std::cerr << "Set OPENAI_API_KEY or OPENAI_APIKEY to run this example.\n";
return 2;
}
const char* model = std::getenv("AX_OPENAI_MODEL");
axllm::OpenAICompatibleClient client(axllm::object({
{"api_key", key},
{"model", model == nullptr || std::string(model).empty() ? "gpt-5.4-mini" : model},
{"model_config", axllm::object({{"temperature", 0}})},
}));
axllm::AxGen program = axllm::ax("context:string, question:string -> answer:string, citations:string[]");
axllm::Value output = program.forward(client, axllm::object({{"context", "Ax uses signatures, ai(), ax(), agent(), flow(), and optimize()."}, {"question", "How should a new developer think about Ax?"}}));
std::cout << axllm::stringify(output) << "\n";
}C++ Adaptive Provider Balancing
Routes equivalent chat traffic using shared reliability, latency, and cost statistics.
- Provider:
openai-compatible - Env:
OPENAI_API_KEY,OPENAI_APIKEY - Level:
advanced - Run:
npm run example -- cpp src/examples/cpp/generation/adaptive_balancer.cpp - Source: src/examples/cpp/generation/adaptive_balancer.cpp
#include "axllm/axllm.hpp"
#include <cstdlib>
#include <iostream>
#include <memory>
#include <vector>
int main() {
const char* raw_key = std::getenv("OPENAI_API_KEY");
if (raw_key == nullptr || std::string(raw_key).empty()) raw_key = std::getenv("OPENAI_APIKEY");
if (raw_key == nullptr || std::string(raw_key).empty()) {
std::cerr << "Set OPENAI_API_KEY or OPENAI_APIKEY to run this example.\n";
return 2;
}
const std::string model = std::getenv("AX_OPENAI_MODEL") == nullptr ? "gpt-5.4-mini" : std::getenv("AX_OPENAI_MODEL");
auto primary = std::make_shared<axllm::OpenAICompatibleClient>(axllm::object({{"api_key", raw_key}, {"model", model}}));
auto backup = std::make_shared<axllm::OpenAICompatibleClient>(axllm::object({{"api_key", raw_key}, {"model", model}}));
auto store = std::make_shared<axllm::AxInMemoryBalancerStatsStore>();
std::vector<std::string> route_keys{"openai-primary", "openai-backup"};
std::vector<std::string> events;
auto strategy = std::make_shared<axllm::AxBalancerAdaptiveStrategy>();
strategy->deadline_ms = 6'000;
strategy->bad_outcome_cost = 0.02;
strategy->expected_tokens = axllm::object({{"promptTokens", 1'200}, {"completionTokens", 300}});
strategy->name_space = "support-summary-v1";
strategy->route_key = [route_keys](const std::shared_ptr<axllm::AxAIService>&, std::size_t index) { return route_keys.at(index); };
strategy->slice = [](axllm::Value context) { return axllm::Core::truthy(axllm::Core::get(axllm::Core::get(context, "options"), "stream")) ? "streaming" : "interactive"; };
strategy->stats_store = store;
strategy->on_routing_event = [&events](axllm::Value event) { events.push_back(axllm::display(axllm::Core::get(event, "type"))); };
axllm::AxBalancerOptions options;
options.strategy = strategy;
axllm::AxBalancer balancer({primary, backup}, options);
auto response = balancer.chat(axllm::object({{"model", model}, {"chat_prompt", axllm::array({axllm::object({{"role", "user"}, {"content", "Summarize why shared routing state matters."}})})}}));
std::cout << axllm::stringify(response) << "\n" << events.size() << " routing events\n";
}