Complete reference for the C++ API.
import mcpplibs.llmapi;
using namespace mcpplibs::llmapi;Client(std::string_view apiKey, std::string_view baseUrl = URL::OpenAI)
Client(const char* apiKey, std::string_view baseUrl = URL::OpenAI)Parameters:
apiKey- API key (can be fromstd::getenv())baseUrl- Base URL (see Providers)
Example:
Client client(std::getenv("OPENAI_API_KEY"), URL::Poe);All configuration methods return Client& for chaining.
Client& model(std::string_view model)Set the model name.
Example:
client.model("gpt-5");All message methods return Client& for chaining.
Client& user(std::string_view content)Add a user message.
Example:
client.user("What is C++?");Client& system(std::string_view content)Add a system message (usually for initial instructions).
Example:
client.system("You are a helpful assistant.");Client& assistant(std::string_view content)Add an assistant message (usually for few-shot examples or manual history).
Note: Normally not needed - responses are auto-saved to history.
Example:
client.assistant("I understand.");Client& add_message(std::string_view role, std::string_view content)Add a message with custom role.
Example:
client.add_message("user", "Hello");Client& clear()Clear all conversation history.
Example:
client.clear();Json request()Execute a non-streaming request. Returns full JSON response. Automatically saves assistant reply to history.
Returns: nlohmann::json object with full API response
Example:
auto response = client.user("Hello").request();
std::println("{}", response["choices"][0]["message"]["content"]);template<StreamCallback Callback>
void request(Callback&& callback)Execute a streaming request. Automatically saves complete assistant reply to history.
Parameters:
callback- Function acceptingstd::string_view(each content chunk)
Example:
client.user("Tell me a story").request([](std::string_view chunk) {
std::print("{}", chunk);
std::cout.flush();
});std::string getAnswer() constGet the last assistant reply from conversation history.
Returns: Last assistant message content, or empty string if none
Example:
client.request();
std::string answer = client.getAnswer();
std::println("Last answer: {}", answer);Json getMessages() constGet full conversation history as JSON array.
Returns: JSON array of all messages
Example:
auto history = client.getMessages();
for (const auto& msg : history) {
std::println("{}: {}", msg["role"], msg["content"]);
}int getMessageCount() constGet total number of messages in conversation history.
Returns: Number of messages
Example:
std::println("Messages: {}", client.getMessageCount());std::string_view getApiKey() constGet the API key.
std::string_view getBaseUrl() constGet the base URL.
std::string_view getModel() constGet the current model name.
template<typename F>
concept StreamCallback = std::invocable<F, std::string_view> &&
std::same_as<std::invoke_result_t<F, std::string_view>, void>;Type constraint for streaming callbacks. Accepts any callable that:
- Takes
std::string_viewparameter - Returns
void
Valid callbacks:
// Lambda
[](std::string_view chunk) { std::print("{}", chunk); }
// Function
void my_callback(std::string_view chunk) { /* ... */ }
// Functor
struct Printer {
void operator()(std::string_view chunk) { /* ... */ }
};using Json = nlohmann::json;The library uses nlohmann/json for JSON handling.
import mcpplibs.llmapi;
import std;
int main() {
using namespace mcpplibs::llmapi;
Client client(std::getenv("OPENAI_API_KEY"), URL::Poe);
// Configure
client.model("gpt-5")
.system("You are a helpful assistant.");
// First question (non-streaming)
client.user("What is C++?");
client.request();
std::println("Answer 1: {}", client.getAnswer());
// Follow-up (streaming) - uses conversation history
client.user("Tell me more");
std::print("Answer 2: ");
client.request([](std::string_view chunk) {
std::print("{}", chunk);
std::cout.flush();
});
std::println("\n");
// Check history
std::println("Total messages: {}", client.getMessageCount());
return 0;
}All methods may throw exceptions:
std::runtime_error- API errors, network errorsstd::invalid_argument- Invalid parametersnlohmann::json::exception- JSON parsing errors
Example:
try {
client.user("Hello").request();
} catch (const std::runtime_error& e) {
std::println("Error: {}", e.what());
}