Installation
Two ways to use it. Drive it from a coding agent (install it as a skill, then describe your model in plain language), or install the R / Python package and call it from code. Install steps for each:
Recommended: generate your sampler in a coding agent, where
generation is the most complete and runs through your Claude or Codex subscription. Then use the
R or Python package to load and run the sampler it writes. The package can
generate too, but it supports fewer models than the coding agent, and its generate() calls the model API directly, so it needs a real API
key (for Anthropic, an sk-ant-api... key) billed per call. A subscription token from
claude setup-token will not work there, since Anthropic no longer allows those on the
direct API. So the package suits the occasional sampler, and the coding agent suits frequent
generation on a subscription. Installing both gives the smoothest workflow.
AI4BayesCode runs through a coding agent, for example Claude Code, Cursor, or Codex. A chat-only LLM like ChatGPT can’t be used, because the agent needs to work with files on your computer and run code to validate the sampler it builds.
The repository root is the skill package. Install it once, reload, then launch it and describe your model.
# clone, then copy in (re-running overwrites any existing install) git clone https://github.com/zjg540066169/AI4BayesCode.git cd AI4BayesCode mkdir -p ~/.claude/skills rsync -a --delete --exclude '.git/' ./ ~/.claude/skills/AI4BayesCode/
Reload Claude Code, then type /AI4BayesCode.
# clone, then copy in (re-running overwrites any existing install) git clone https://github.com/zjg540066169/AI4BayesCode.git cd AI4BayesCode mkdir -p ~/.codex/skills rsync -a --delete --exclude '.git/' ./ ~/.codex/skills/AI4BayesCode/
Restart Codex, then type /Ai4bayescode (Codex lowercases the skill name, so only the first letter is capitalized).
# clone the repo (no skills dir needed) git clone https://github.com/zjg540066169/AI4BayesCode.git # then tell your coding agent: Read AI4BayesCode/start.md first, then: <your model description>
No slash command needed. Cursor, Windsurf, Cline, aider, or any coding agent (one with local-file & code access) launches by reading start.md, which routes you to generate-vs-design and loads the rest on demand.
Also recommended: install the R or Python package too (the R / Python package tab). Then the sampler the agent writes finds the AI4BayesCode library on your computer on its own, and you won’t have to point it at any folder by hand.
After install, launch it. Use the slash command in Claude Code / Codex,
or tell any other coding agent to "Read AI4BayesCode/start.md first". It first
asks what you want to
do: generate a sampler (compose existing blocks for your model) or
design a new block (build a new sampling primitive). Choose
generate a sampler, then describe your model in plain typeable text
(spell Greek out: beta, sigma):
# Claude Code: /AI4BayesCode · Codex: /Ai4bayescode # Cursor / other coding agent: "Read AI4BayesCode/start.md first" # then choose "generate a sampler", and describe the model: y ~ N(Xbeta, sigma^2) with Jeffreys prior p(sigma^2) propto 1/sigma^2
The block-design flow is the other path, for building a new sampling primitive when
no existing block fits. Pure-C++ users need only a C++17 compiler. The bundled
include/ headers are the install.
The same library ships as installable R and Python
packages, self-contained (C++ headers + skill corpus travel inside). Describe the
model in plain text and generate() writes and validates the sampler.
# needs a C++17 compiler + Rcpp, RcppArmadillo remotes::install_github( "zjg540066169/AI4BayesCode", subdir = "r-pkg") library(AI4BayesCode) ai4bayescode_set_key("sk-ant-...") # replace "sk-ant-..." with your own API key ai4bayescode_generate( "y ~ N(Xbeta, sigma^2) with Jeffreys prior p(sigma^2) propto 1/sigma^2") # or, instead of set_key, pass the key in the call: # ai4bayescode_generate( # "y ~ N(Xbeta, sigma^2) with Jeffreys prior p(sigma^2) propto 1/sigma^2", # API_key = "sk-ant-...")
# Python >= 3.11; a C++ compiler; brew install armadillo pip install anthropic # the LLM client that generate() calls pip install "git+https://github.com/zjg540066169/AI4BayesCode.git#subdirectory=python" # then, in Python: import AI4BayesCode AI4BayesCode.set_key("sk-ant-...") # replace "sk-ant-..." with your own API key AI4BayesCode.generate( "y ~ N(Xbeta, sigma^2) with Jeffreys prior p(sigma^2) propto 1/sigma^2") # or, instead of set_key, pass the key in the call: # AI4BayesCode.generate( # "y ~ N(Xbeta, sigma^2) with Jeffreys prior p(sigma^2) propto 1/sigma^2", # API_key="sk-ant-...")
"sk-ant-..." is a placeholder. Replace it with your own LLM
provider key (your Anthropic or OpenAI key).
ai4bayescode_generate() (R) / AI4BayesCode.generate() (Python)
uses that key to write the sampler. Set it once with
ai4bayescode_set_key() / AI4BayesCode.set_key(), or pass
API_key= to generate() directly. Or run a built-in example:
ai4bayescode_example("GaussianLocationScale").
generate() saves the sampler as a .cpp file. Load it with
ai4bayescode_source() / AI4BayesCode.source(), which compiles
against the bundled library, with no paths to configure, then run
ai4bayescode_doc() / AI4BayesCode.doc() to print a usage card:
what the sampler takes as input and how to call it.
Beyond the built-in blocks, you can also use contributed blocks made by other users, which work just like built-in ones. See the Blocks page for the catalogue and how to install them.