Flows Flows — C++ examples backed by real provider calls. cpp examples examples/flows src/examples/cpp/flows example Flows

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++ Sequential Flow

Runs a two-step Ax flow against OpenAI.

C++
#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 step = axllm::ax("documentText:string -> summaryText:string");
  axllm::AxFlow program = axllm::flow(axllm::object({{"id", "examples.sequentialFlow"}}))
      .execute("step", step)
      .map("note", [](axllm::Value) { return axllm::object({{"note", "Mapped flow state after the provider-backed step."}}); })
      .returns(axllm::object({{"step", "step"}, {"note", "note"}}));
  axllm::Value output = program.forward(client, axllm::object({{"documentText", "Ax gives developers signatures, provider clients, agents, flows, tracing, and optimization."}}));
  std::cout << axllm::stringify(output) << "\n";
}

C++ Branching Flow

Routes a classification through follow-up flow logic backed by OpenAI.

C++
#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 classifier =
      axllm::ax("request:string -> route:class \"support, sales, engineering\"");
  axllm::AxGen responder = axllm::ax("request:string, route:string -> response:string");
  axllm::AxFlow program = axllm::flow(axllm::object({{"id", "examples.branchFlow"}}))
      .execute("classifier", classifier,
               axllm::object({{"reads", axllm::array({"request"})},
                              {"writes", axllm::array({"classifierResult", "route"})}}))
      .execute("responder", responder,
               axllm::object({{"reads", axllm::array({"request", "route"})},
                              {"writes", axllm::array({"responderResult", "response"})}}))
      .returns(axllm::object({{"route", "route"}, {"response", "response"}}));
  axllm::Value output = program.forward(client, axllm::object({{"request", "A customer says checkout is down for their enterprise account."}}));
  std::cout << axllm::stringify(output) << "\n";
}

C++ Parallel Flow

Runs two independent OpenAI-backed steps in parallel before joining their results.

C++
#include "axllm/axllm.hpp"
#include <cstdlib>
#include <iostream>

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 research = axllm::ax("topicText:string -> factList:string[]");
  axllm::AxGen audience = axllm::ax("topicText:string -> audienceAngle:string");
  axllm::AxGen join = axllm::ax("factList:string[], audienceAngle:string -> briefText:string");
  axllm::AxFlow program = axllm::flow(axllm::object({{"id", "examples.parallelFlow"}}))
      .execute("research", research,
               axllm::object({{"reads", axllm::array({"topicText"})},
                              {"writes", axllm::array({"researchResult", "factList"})}}))
      .execute("audience", audience,
               axllm::object({{"reads", axllm::array({"topicText"})},
                              {"writes", axllm::array({"audienceResult", "audienceAngle"})}}))
      .execute("join", join,
               axllm::object({{"reads", axllm::array({"factList", "audienceAngle"})},
                              {"writes", axllm::array({"joinResult", "briefText"})}}))
      .returns(axllm::object({{"briefText", "briefText"}}));
  axllm::Value output = program.forward(
      client,
      axllm::object({{"topicText", "Why typed contracts make multi-step LLM systems easier to maintain"}}));
  std::cout << axllm::stringify(output) << "\n";
}

C++ Composed Flow

Composes multiple typed programs into one OpenAI-backed flow.

C++
#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 step = axllm::ax("topic:string -> outline:string[]");
  axllm::AxFlow program = axllm::flow(axllm::object({{"id", "examples.composedFlow"}}))
      .execute("step", step)
      .map("note", [](axllm::Value) { return axllm::object({{"note", "Mapped flow state after the provider-backed step."}}); })
      .returns(axllm::object({{"step", "step"}, {"note", "note"}}));
  axllm::Value output = program.forward(client, axllm::object({{"topic", "How Ax moves from typed generation to agents, flows, and optimization"}}));
  std::cout << axllm::stringify(output) << "\n";
}

C++ Refinement Flow

Drafts, critiques, and revises an answer through three OpenAI-backed steps.

C++
#include "axllm/axllm.hpp"
#include <cstdlib>
#include <iostream>

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 draft = axllm::ax("topicText:string -> draftText:string");
  axllm::AxGen critique = axllm::ax("draftText:string -> critiqueText:string");
  axllm::AxGen revise = axllm::ax("draftText:string, critiqueText:string -> revisedText:string");
  axllm::AxFlow program = axllm::flow(axllm::object({{"id", "examples.refineFlow"}}))
      .execute("draft", draft,
               axllm::object({{"reads", axllm::array({"topicText"})},
                              {"writes", axllm::array({"draftResult", "draftText"})}}))
      .execute("critique", critique,
               axllm::object({{"reads", axllm::array({"draftText"})},
                              {"writes", axllm::array({"critiqueResult", "critiqueText"})}}))
      .execute("revise", revise,
               axllm::object({{"reads", axllm::array({"draftText", "critiqueText"})},
                              {"writes", axllm::array({"reviseResult", "revisedText"})}}))
      .returns(axllm::object({{"revisedText", "revisedText"}}));
  axllm::Value output = program.forward(
      client,
      axllm::object({{"topicText", "Explain automatic flow parallelism to a backend engineer."}}));
  std::cout << axllm::stringify(output) << "\n";
}
Docs