Installation
Two ways to use AI4BayesCode:
New here? Install both. Generate your sampler with a coding agent, then run it with the R or Python package.
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, copy in, then delete the clone (re-run to update) git clone https://github.com/zjg540066169/AI4BayesCode.git cd AI4BayesCode mkdir -p ~/.claude/skills rsync -a --delete --exclude '.git/' ./ ~/.claude/skills/AI4BayesCode/ cd .. && rm -rf AI4BayesCode
Reload Claude Code, then type /AI4BayesCode.
# clone, copy in, then delete the clone (re-run to update) git clone https://github.com/zjg540066169/AI4BayesCode.git cd AI4BayesCode mkdir -p ~/.codex/skills rsync -a --delete --exclude '.git/' ./ ~/.codex/skills/AI4BayesCode/ cd .. && rm -rf 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.
With the R or Python package installed, the sampler the agent writes finds the AI4BayesCode library on its own, so you never have to point it at a 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. It runs only in a coding agent, not in the R or Python
package. 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 (Anthropic or OpenAI). It must be a real API key billed per call
(for Anthropic, an sk-ant-api... key), not a subscription token from
claude setup-token, which Anthropic no longer allows on the direct API.
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.