Elixir client for System One decision models: TypeSafe Jev, Cloudflare Clef, OpenAI's Decisions API, Google's Gemini Decisions API, OpenRouter's decisions endpoint, self-hosted Flide servers, and anything else that speaks the /v1/systemone wire format (for example CLM).

You send a state (any JSON) and a map of typed questions; you get back typed answers with their full probability distributions, never collapsed to a single pick. That is what a router or a gate needs: top-k over a Choice, a threshold on a Noul, a level cut on a Score.

  • One behaviour, SystemOneClient, with evaluate(state, questions, opts).
  • SystemOneClient.HTTP for real calls; SystemOneClient.Stub for tests, no network.
  • Seven providers behind one option; url: and model: override any of them.
  • Retries on 429, 529, 502, 503, 504 and transport errors, honouring Retry-After.
  • Never raises for network conditions; tagged error tuples at the boundary.
  • Telemetry without leaking state, questions, or keys.
  • Depends on req, jason, telemetry only. Apache 2.0.

Install

def deps do
  [{:system_one_client, "~> 0.5"}]
end

Use

questions = %{
  "needs_tool" => %{
    "type" => "noul",
    "instructions" => "Does the next step need one of the actions in `available_actions`?"
  },
  "tool" => %{
    "type" => "choice",
    "instructions" => "Which action should run next to make progress on `request`?",
    "criteria" => %{"add" => "Adds two numbers", "gcd" => "Greatest common divisor", "none" => "No action fits"}
  },
  "depth" => %{
    "type" => "score",
    "instructions" => "How much reasoning does `request` need?",
    "criteria" => ["One direct step", "A few dependent steps", "A long chain or a known trap"]
  }
}

state = %{request: "what is the gcd of 1071 and 462", available_actions: ["add", "gcd"]}

{:ok, answers, meta} = SystemOneClient.evaluate(state, questions)

answers["tool"]
#=> %SystemOneClient.Answer.Choice{choice: "gcd", confidence: 0.97,
#     probabilities: %{"gcd" => 0.97, "add" => 0.02, "none" => 0.01}}
answers["needs_tool"]   #=> %SystemOneClient.Answer.Noul{noul: 0.96}
answers["depth"]        #=> %SystemOneClient.Answer.Score{score: 0.1, probabilities: %{...}, confidence: 0.9, legend: ...}
meta                    #=> %{model: "jev-1.13.0", usage: %{...}, latency_ms: 134, provider: :typesafe}

Question maps are the API's wire format verbatim; this library adds no schema of its own. See TypeSafe's primitives for noul, choice and score.

Providers

provider:EndpointKeyDefault model
:typesafe (default)https://api.typesafe.ai/v1/systemoneapi_key: or TYPESAFE_API_KEYjev-latest
:cloudflarehttps://api.cloudflare.com/client/v4/accounts/{account_id}/ai/run/@cf/cloudflare/clefapi_key: or CLOUDFLARE_API_TOKEN; account_id: or CLOUDFLARE_ACCOUNT_IDclef (model: "clef-flash" for the 9B)
:openaihttps://api.openai.com/v1/decisionsapi_key: or OPENAI_API_KEYgpt-6-luna
:openrouterhttps://openrouter.ai/api/alpha/decisionsapi_key: or OPENROUTER_API_KEYtypesafe/jev-1.13
:geminihttps://generativelanguage.googleapis.com/v1beta/decisionsapi_key: or GEMINI_API_KEYgemini-decision-1
:flideurl: or FLIDE_URL, else http://localhost:8000/v1/systemoneoptional: api_key: or FLIDE_API_KEYthe model the server runs
:azure_foundryurl: or AZURE_FOUNDRY_ENDPOINT (+ /providers/microsoft/v1/systemone)api_key: or AZURE_FOUNDRY_API_KEY (api-key header); bearer_token: or AZURE_FOUNDRY_BEARER_TOKENnone: model: is required
:compatibleurl: (required)optionalnone
SystemOneClient.evaluate(state, questions, provider: :cloudflare, model: "clef-flash")
SystemOneClient.evaluate(state, questions, provider: :openai)
SystemOneClient.evaluate(state, questions, provider: :gemini)
SystemOneClient.evaluate(state, questions, provider: :flide, url: "http://gpu-box:8000/v1/systemone")
SystemOneClient.evaluate(state, questions, provider: :compatible, url: "http://localhost:8700/v1/systemone")

Cloudflare's result envelope is unwrapped for you; a failed envelope returns {:error, {:provider_error, errors}}.

OpenAI Decisions

OpenAI's Decisions API asks the same three kinds of question with a different wire format, so provider: :openai translates in both directions and you keep writing the same questions and get the same structs back:

  • noul is sent as a predicate. Predicates have no criteria field, so true / false criteria are appended to the instructions as "True when: ..." and "False when: ...".
  • choice criteria become choices, and score levels become levels labelled "Level 0", "Level 1", ... with your text as the description.
  • state becomes the text input: strings pass through, anything else is JSON-encoded. Non-string instructions and descriptions are JSON-encoded too.
  • Score probabilities are keyed by level index ("0", "1", ...) and legend carries your level text, the same as TypeSafe.
  • OpenAI can refuse a single question. That answer comes back as %SystemOneClient.Answer.Refusal{} under its id, and the other answers are unaffected.
  • A choice or score question without criteria is rejected before the request, as {:error, {:invalid_question, id, :missing_criteria}}.
  • A key that belongs to several organizations or projects can select one with organization: (header OpenAI-Organization, else OPENAI_ORG_ID) and project: (header OpenAI-Project, else OPENAI_PROJECT_ID). Without them, usage and billing go to the key's default. Unset or empty values send no header, and neither value appears in telemetry or error terms.
  • For regional processing, pass url: "https://us.api.openai.com/v1/decisions" (or eu.).

Flide

Flide is a self-hosted inference server for decision models (flide serve --model ...). It speaks the System One format, so nothing is translated:

  • The default URL is Flide's own, http://localhost:8000/v1/systemone; pass url: or set FLIDE_URL for another host. The option wins over the variable.
  • A server runs one model, so no model is sent unless you pass model:. If you do, it must be the served model's name or one of its aliases, or Flide answers 404. meta.model is the name the server reports.
  • Flide has no authentication of its own. api_key: or FLIDE_API_KEY is sent as a bearer token if you put it behind a proxy that wants one.
  • images: is supported, for servers running a model that reads images.
  • Validation errors come back as {:error, {:http, 422, %{"detail" => [...]}}}.

Microsoft Foundry

provider: :azure_foundry calls Microsoft-Decision-1.1 deployed in Microsoft Foundry. It speaks the System One format, so nothing is translated.

SystemOneClient.evaluate(state, questions,
  provider: :azure_foundry,
  url: "https://my-resource.services.ai.azure.com/providers/microsoft/v1/systemone",
  model: "my-deployment",
  api_key: key
)
  • The endpoint belongs to your Azure resource. Pass the full url:, or set AZURE_FOUNDRY_ENDPOINT to the resource endpoint and /providers/microsoft/v1/systemone is appended. Without either you get {:error, :missing_url}.
  • model: is your deployment name, not the model name, and is required ({:error, :missing_model} otherwise). meta.model is the model name the service reports.
  • Keys are sent in the api-key header. For Microsoft Entra ID, pass a token as bearer_token: (scope https://cognitiveservices.azure.com/.default); it is sent as Authorization: Bearer. This library does not fetch Entra tokens. Precedence: bearer_token:, api_key:, AZURE_FOUNDRY_API_KEY, AZURE_FOUNDRY_BEARER_TOKEN.
  • images: is not supported yet ({:error, {:unsupported_option, :images, :azure_foundry}}). Foundry takes images as top-level assets referenced as image:<name>, which this library does not map yet.
  • Not verified against the service: the usage shape, error bodies, whether an api-version query parameter is needed (if so, include it in url:), and whether 429 responses carry Retry-After. The AZURE_FOUNDRY_* variable names are this library's own.

Gemini Decisions

Google's Gemini Decisions API uses nearly the System One format, so provider: :gemini only reshapes the request; answers are parsed as they arrive.

  • The key is sent in the x-goog-api-key header, not as a bearer token.
  • state becomes the text input: strings pass through, anything else is JSON-encoded. Non-string instructions and descriptions are JSON-encoded too. Gemini rejects an empty input with a 400.
  • noul criteria true / false are sent as true_description / false_description.
  • A choice or score question without criteria is rejected before the request, as {:error, {:invalid_question, id, :missing_criteria}}.
  • score is the probability-weighted average of the level indices (0 up to the highest index), the same as OpenAI.
  • meta.usage is Gemini's own shape: total_input_tokens, input_tokens_by_modality, total_output_tokens (always 0) and total_tokens.
  • Gemini sorts choice options by key and breaks exact ties towards the first key, so a catch-all option does not stay last; do not rely on option order.
  • A safety block comes back as {:error, {:http, 400, %{"error" => %{"code" => "safety", ...}}}}.
  • Server-side limits: 1 to 1,000 questions per request, question ids matching [A-Za-z0-9_-]+ up to 64 characters, and 2 to 255 options or levels per question.

Images

OpenAI's Decisions API, Gemini's Decisions API, Cloudflare's Clef and Flide servers running a vision model can judge images as well as text. Pass them as base64 data URLs in the images: option; SystemOneClient.image_data_url/2 builds one from bytes:

photo = SystemOneClient.image_data_url(File.read!("product.png"), "image/png")

SystemOneClient.evaluate(
  "Customer says the item arrived broken.",
  %{"visible_damage" => %{"type" => "noul", "instructions" => "Does the product show visible damage?"}},
  provider: :openai,
  images: [photo]
)

All of them see the images first and then the state. For OpenAI the client builds a user message with input_image parts followed by the state as text; for Gemini it sends image blocks followed by a text block; for Clef and Flide it sends the top-level images array.

Images are checked before any request:

  • Every entry must be a data:image/...;base64, URL. Remote http(s) URLs are rejected, OpenAI, Clef and Flide do not accept them; Gemini does, but this option does not pass them through yet.
  • Gemini takes PNG, JPEG, WebP, HEIC, HEIF, GIF, BMP and TIFF, up to 100 MiB in total.
  • Flide has no local checks beyond the data URL format; what it accepts depends on the model it serves.
  • Clef allows at most 4 PNG, JPEG or WebP images, 4 MiB each and 8 MiB in total once decoded.
  • Providers without image support (:typesafe, :openrouter, :compatible) return {:error, {:unsupported_option, :images, provider}} rather than dropping them.

Options

OptionDefaultMeaning
provider:typesafesee above
api_key, account_id, url, modelprovider defaultsexplicit values always win
organization, projectOPENAI_ORG_ID, OPENAI_PROJECT_ID:openai only; sent as OpenAI-Organization and OpenAI-Project
receive_timeout2_000 msper attempt
max_retries2on 429, 529, 502, 503, 504 and transport errors
retry_delay_ms250base of the exponential backoff; Retry-After / retry-after-ms win when present
clientapp env :client, else HTTPimplementation module (use SystemOneClient.Stub in tests)
plugnoneReq plug, for Req.Test in your own tests
envSystem.get_env()replace the environment (tests)
imagesnonebase64 image data URLs (:openai, :gemini, :cloudflare, :flide); see Images

Errors are tagged tuples:

{:error, :missing_api_key | :missing_account_id | :missing_url | {:unknown_provider, p}}
{:error, {:invalid_question, id, reason}}
{:error, {:http, status, body} | {:transport, reason}}
{:error, {:malformed_answers, reason} | {:provider_error, errors}}
{:error, {:unsupported_option, :images, provider} | {:invalid_image, index, reason}}
{:error, {:too_many_images, count, max} | {:images_too_large, bytes, max}}

Testing your code

# config/test.exs
config :system_one_client, client: SystemOneClient.Stub

# in a test
alias SystemOneClient.Answer.{Choice, Noul}

{:ok, answers, _} =
  SystemOneClient.evaluate(state, questions,
    answers: %{
      "needs_tool" => %Noul{noul: 0.9},
      "tool" => %Choice{choice: "gcd", probabilities: %{"gcd" => 0.9, "add" => 0.1}, confidence: 0.9}
    })

answers: takes a map (structs or raw API maps) or a fn state, questions -> map | {:error, _} end. For HTTP-level tests pass plug: {Req.Test, MyTest} and stub with Req.Test.stub/2.

Telemetry

[:system_one_client, :request, :start | :stop | :exception] via :telemetry.span/3. Metadata: provider, model, question_count, image_count; on :stop also status (:ok | :error), latency_ms, and error (a kind such as {:http, 429} or :transport). State, question text, bodies and keys are never included.

Origin

Extracted from an experiment that used Jev as a tool and model router inside a Jido AI agent; the experiment, its benchmarks and the transformer that consumes this client live on schainks/jido, branch experiment/jev-tool-routing.

Running the tests

With Elixir installed: mix test. Without: scripts/run.sh test runs them in the hexpm/elixir:1.20.4 Docker image.

Contributing

Bug fixes, new providers and docs improvements are welcome. See CONTRIBUTING.md for setup, the checks CI runs, and how to add a provider.