Z3 Constraint Solver API: SMT, Logic Puzzles & Proofs
Pricing
Pay per event
Z3 Constraint Solver API: SMT, Logic Puzzles & Proofs
Solve logic and constraint problems with the Z3 theorem prover. Write constraints the plain way (x + y <= 10, a => b, distinct) or send SMT-LIB 2. Get values, all solutions, the constraints that conflict, optimal values, or a proof with a counterexample. For AI agents and developers.
Pricing
Pay per event
Rating
0.0
(0)
Developer
Jumble Mog
Maintained by CommunityActor stats
0
Bookmarked
2
Total users
1
Monthly active users
3 hours ago
Last modified
Categories
Features
Share
Solve logic and constraint problems with Z3, the theorem prover from Microsoft Research, without installing anything. Write the problem the way you would on paper (x, y: Int in 0..10, x + y == 7, rain => umbrella, distinct(a, b, c)) or send an SMT-LIB 2 script. You get back values that satisfy every constraint, or several or all solutions, or, when there is no solution, the exact constraints that conflict. Add minimize or maximize for the best solution, prefer for soft constraints, and prove to have a claim proved, or refuted with a counterexample. Made for AI agents that need a check they can trust instead of a guess, developers who want an SMT solver behind an API, people who verify rules, configurations and policies, and anyone with a scheduling problem or a logic puzzle.
What it does
- Finds values that satisfy your constraints, over whole numbers, real numbers (exact fractions, not floating point), true/false values and fixed-width bit-vectors, with lists and grids of variables.
- Lists several or all solutions: ask for up to 1,000, and the result says whether those are all there are.
- Explains "no solution": the constraints that can't all hold together, by name and line, reduced so that every one listed is needed for the conflict. Remove or relax any one of them and the rest can hold.
- Optimizes:
minimizeormaximizeany expression (several in order of priority), and soft constraints with weights (prefer x > 3 weight 2) that are met as far as possible. - Proves claims:
prove x*y >= xanswers proved (true for every value the constraints allow) or disproved with a counterexample. - Runs SMT-LIB 2 scripts as they are:
declare-const,declare-fun,define-fun, datatypes, arrays, quantifiers,pushandpop,check-sat,get-model,get-value,get-unsat-core,minimize,maximize,assert-soft. The model, the core of named assertions and the objectives are fetched for you when the script doesn't ask, and returned as JSON values next to Z3's own output. - Translates plain constraints to an SMT-LIB 2 file you can run in any SMT solver (objectives and soft constraints are written with Z3's
minimize,maximizeandassert-softcommands). - Clear errors: a problem that can't be read comes back with the line and what to change (
Line 2: cots isn't declared. Declare it first, such as "cots: Int". Did you mean cost?) and is never charged. - Many problems in one run: up to 200, each with its own result.
Who it's for
- AI agents and LLM apps: language models are unreliable at multi-step logic, arithmetic constraints and "is this always true?" questions. An agent can write the constraints, have them solved here and answer from a real model, a real conflict or a real proof.
- Developers: a hosted Z3 and SMT-LIB API for apps, automations and no-code flows, billed per problem with no monthly plan.
- Rules and policy work: check that access rules, pricing rules, feature flags or configuration options can't contradict each other, and see which ones do.
- Planning and scheduling: timetables, shifts, assignments and orderings with hard constraints and preferences.
- Verification and security: bit-vector arithmetic, overflow checks, equivalence of two expressions, counterexamples for a property.
- Students, teachers and puzzle solvers: logic and discrete math homework, cryptarithms, Sudoku, N-queens, Zebra puzzles.
How to use it
- Write the problem in Problem. The form starts with a small scheduling problem, so you can click Start right away.
- For more than one solution, set Solutions to find.
- Click Start. The result has the status, a summary in plain words, a value for every variable, and the conflict, objectives or claims when the problem has them.
You can also run it through the Apify API, from Make, n8n, Zapier and other integrations, or as a tool for AI agents.
Writing a problem
One statement per line, or several separated by semicolons. # starts a comment.
# Three one-hour meetings between 9:00 and 17:00, finished as early as possibledesign, review, demo: Int in 9..16distinct(design, review, demo)design < review # the review needs the designreview < demoalice is free in the morning: design < 12nobody meets at lunch: design != 12 and review != 12 and demo != 12the room is booked at 10: design != 10 and review != 10 and demo != 10minimize demo
| Write | Meaning |
|---|---|
x, y: Int | Whole-number variables. Also Real, Bool and BitVec(8) (an 8-bit number). Int x, y works too. |
x: Int in 0..9, r: Real in 0.5..1.5, d: Int in [1, 3, 5] | A variable with a range (both ends included) or a list of allowed values. |
q[8]: Int, q[1..8]: Int, cell[9][9]: Int in 1..9 | A list of variables counted from 0, a list counted from 1 to 8, and a grid. |
n = 8, total = x + y, late = finish > 17 | A name for a value, an expression or a condition. Named expressions come back in named. |
x + 2*y <= 14, a != b, x % 3 == 1, x^2 + y^2 == 25 | Constraints with + - * / % ^ and == != < <= > >=. A single = between expressions also means equals. |
x in 1..5, x not in [2, 4], x in a..b | A value inside a range or a list. The ends of a range can be variables here. |
p and q, p or q, not p, p => q, p <=> q | Logic. &&, || and ! work too. |
x if c else y, ite(c, x, y) | One of two values, by a condition. |
budget: 3*x + y <= 40 | A constraint with a name, shown when constraints conflict. |
for i in 1..n-1: q[i] < q[i+1] | A constraint for each value. Several loops and a condition: for i in 1..n, j in 1..n if i < j: ... |
distinct(q), sum(q) == 20, count(a, b, c) >= 2 | Functions take values, lists or for expressions: distinct(q[i] + i for i in 1..n). |
abs min max product all any xor | More functions. atmost(2, a, b, c), atleast(...) and exactly(...) count true conditions. |
q[k] == 7 | An item picked by a variable k. |
minimize cost, maximize value | Objectives. With several, the first matters most. |
prefer x > 3, prefer not late weight 5 | Soft constraints: as much total weight as possible is met. |
prove x*y >= x | A claim to prove from the constraints. |
Between fixed numbers / is the exact fraction (1/3 is a third, and 0.1 is a tenth). Between whole-number variables it is whole-number division; declare a Real for fractions. Bit-vectors compare as signed numbers; ult, ule, ugt, uge, udiv, urem and lshr are the unsigned versions, xor(a, b) is exclusive-or, and &, |, ~, << and >> work on bits. int(x) and real(x) convert.
SMT-LIB 2 is read when the problem starts with a bracket:
(declare-const x Int)(declare-const y Int)(assert (! (> x 5) :named big))(assert (! (< (+ x y) 3) :named small))(assert (! (> y 0) :named positive))(check-sat)
Examples
A schedule. The form's example answers "Optimal: minimize demo gives 13, with design = 9, review = 11, demo = 13."
A puzzle. SEND + MORE = MONEY, each letter a different digit:
S,E,N,D,M,O,R,Y: Int in 0..9distinct(S,E,N,D,M,O,R,Y); S != 0; M != 01000*S + 100*E + 10*N + D + 1000*M + 100*O + 10*R + E = 10000*M + 1000*O + 100*N + 10*E + Y
"Satisfiable: S = 9, E = 5, N = 6, D = 7, M = 1, O = 0, R = 8, Y = 2." With solutions set to 2 it adds "That is the only solution."
All solutions. Six queens on a 6 by 6 board, none attacking another, with solutions set to 10:
n = 6q[1..n]: Int in 1..ndistinct(q)distinct(q[i] + i for i in 1..n)distinct(q[i] - i for i in 1..n)
"Satisfiable: q = [4, 1, 5, 2, 6, 3]. All 4 solutions found."
Preferences. A knapsack: w = [12, 7, 11, 8, 9], v = [24, 13, 23, 15, 16], take[5]: Bool, weight = sum(w[i] * ite(take[i], 1, 0) for i in 0..4), value = sum(v[i] * ite(take[i], 1, 0) for i in 0..4), weight <= 26, maximize value gives "Optimal: maximize value gives 51, with take = [false, true, true, true, false]." and named holds weight 26 and value 51.
How-to examples
The result lines are from runs of these inputs.
How to find which rules conflict
{"problem": "x, y: Int in 0..10\nbudget: x + y <= 5\nx >= 4\nbig y: y >= 3\ny != 7"}
Result: "No solution: these 3 constraints can't all hold together: budget (line 2); x >= 4 (line 3); big y (line 4)." The fourth constraint, y != 7, isn't part of the conflict and isn't listed. conflict holds the same as data: [{"line": 2, "name": "budget", "constraint": "budget: x + y <= 5"}, ...].
How to prove a claim or get a counterexample
{"problem": "x, y: Int\nx > 0; y > 0\nprove x*y >= x\nprove x + y > 2"}
Result: "Proved: x*y >= x holds for every value the constraints allow. Not true: x + y > 2 fails for x = 1, y = 1." Each claim is in claims with its result and counterexample.
How to run an SMT-LIB 2 script online
{"problem": "(declare-const x Int)\n(declare-const y Int)\n(assert (! (> x 5) :named big))\n(assert (! (< (+ x y) 3) :named small))\n(assert (! (> y 0) :named positive))\n(check-sat)"}
Result: "No solution: these 3 constraints can't all hold together: big; small; positive." The unsatisfiable core was fetched without the script asking for it. For a satisfiable script, values holds the model: (declare-const x Int)(declare-const p Bool)(assert (and (> x 5) (< x 7) p)) gives "Satisfiable: x = 6, p = true."
How to let an AI agent check its reasoning
Add this Actor to the Apify MCP server with https://mcp.apify.com/?tools=jumblemog/z3-constraint-solver. The agent writes the facts and rules as constraints, runs them, and reads status, values and conflict: a claim comes back proved or with a counterexample, and a set of rules that can't all hold comes back with the ones that clash.
FAQ
What is Z3? An SMT solver (satisfiability modulo theories) from Microsoft Research, used in program verification, security research and planning. It decides whether a set of logical and arithmetic constraints can hold, and finds values when they can.
Do I need to know SMT-LIB? No. Plain constraints cover most problems, and each one is also translated to SMT-LIB 2 (smtlibUrl). If you have SMT-LIB 2 already, send it as it is.
What does "unknown" mean? Z3 found neither a solution nor a proof that there is none within the time limit, or the problem is of a kind no solver can always decide (products of variables over whole numbers, for one). Bounds on the variables (x: Int in -1000..1000) often turn such a problem into one Z3 answers quickly.
Is a solution always the only one? No. values is one solution. Set solutions above 1 to see more; solutionsComplete says whether the list is all of them.
How is it different from an optimization solver? Linear and integer programming solvers are faster on large linear models. Z3 takes logic (=>, or, not), products of variables, bit-vectors and proofs, which those don't.
Input
| Field | What it is |
|---|---|
problem | The problem: plain constraints or an SMT-LIB 2 script. example runs the built-in example. |
solutions | How many different solutions to return (default 1, up to 1,000; plain constraints). |
title | A title for the result. Empty: the problem's first comment line. |
format | auto (default: SMT-LIB when the problem starts with a bracket), plain or smtlib. |
timeLimitSeconds | How long Z3 may work on one problem (default 15, up to 300). |
problems | Several problems: a list of problems as text, or of objects with their own problem and any field above. |
Output
One dataset item per problem, also saved together in the run's OUTPUT record:
{"job": 1,"title": "Three one-hour meetings between 9:00 and 17:00, finished as early as possible","status": "optimal","format": "plain","summary": "Optimal: minimize demo gives 13, with design = 9, review = 11, demo = 13.","values": {"design": 9, "review": 11, "demo": 13},"objectives": [{"objective": "minimize demo", "sense": "minimize", "line": 9, "value": 13, "unbounded": false, "note": null}],"conflict": null, "claims": null, "solutions": null, "softConstraints": null, "named": null,"counts": {"variables": 3, "constraints": 6, "objectives": 1, "softConstraints": 0, "claims": 0},"smtlibUrl": "https://api.apify.com/v2/key-value-stores/.../records/problem-001.smt2","report": "# Three one-hour meetings ...","solverSeconds": 0.13, "notes": [], "error": null}
status is satisfiable, unsatisfiable, optimal, unbounded, proved, disproved, unknown (with reasonUnknown) or failed (the problem couldn't be read; see error). Whole numbers come back as numbers (as text beyond 2^53, which JSON numbers can't hold exactly), exact decimals as numbers, other fractions as text such as "1/3", irrational roots as a decimal ending in ..., and bit-vectors as unsigned numbers. An SMT-LIB script's result also has checks (the answer of each check-sat), functions (the model's function definitions) and output (what Z3 printed).
How it works
Plain constraints are read by a small parser: Python's own grammar for the expressions, with ranges (1..9), => and <=> rewritten first. The syntax tree is then evaluated by this Actor's own code into Z3 terms, name by name, so a problem can use the language's functions and nothing else of Python. Z3 solves it in a separate process that runs as an unprivileged user in an empty folder, with limits on time and memory and no permission to write files. For a problem with no solution, Z3's unsatisfiable core is reduced by leaving out each constraint in turn. SMT-LIB 2 scripts are checked first (only solver commands; nothing that reads or writes files) and run by the z3 program in the same sandbox.
Pricing
You pay per problem solved, at the price shown on this page: a solution, a conflict, an optimum or a proof. One charge covers 15 seconds of solver time, which is the default time limit, and most problems take a few milliseconds. With a longer time limit, each further started 15 seconds is one more charge. A problem Z3 can't decide within the limits is a result and counts; a problem that can't be read is never charged. If you set a maximum cost per run, the Actor shortens the time limit to stay within it.
Tips and limits
- Give variables bounds when you can.
x: Int in 0..1000is easier for Z3 than a barex: Int, above all when variables are multiplied together. - Name the constraints you care about (
budget: ...), so a conflict reads well. - Products of variables over whole numbers (
x*y*z == 42) can be undecidable; such problems may come backunknown. - Real numbers are exact:
0.1 + 0.2 == 0.3is true here. - Limits: 5,000 lines, 400,000 characters and 200,000 variables per problem, two million terms after loops are expanded, a few thousand terms in one written-out sum (use
sum(... for ...)for longer ones), 300 seconds per problem, 200 problems per run. SMT-LIB scripts up to 2 MB; commands that read or write files (include, output-channel options) are refused. - Not there: text (string) variables and quantifiers in plain constraints (SMT-LIB scripts can use both), and Z3's Python API as input.
For AI agents and developers
This Actor works with the Apify MCP server, so AI agents can call it as a tool. A direct API call that waits for the answer:
curl -X POST "https://api.apify.com/v2/acts/jumblemog~z3-constraint-solver/run-sync-get-dataset-items?token=YOUR_TOKEN" \-H "Content-Type: application/json" \-d '{"problem": "x, y: Int in 0..10\nx + y == 7\nx - y == 3"}'
The item's summary is "Satisfiable: x = 5, y = 2." and values is {"x": 5, "y": 2}. run-sync instead returns the run's OUTPUT: the status, how many problems were solved and every result as JSON.
In Python, with the apify-client package, checking three rule sets in one run:
from apify_client import ApifyClientclient = ApifyClient("YOUR_TOKEN")run = client.actor("jumblemog/z3-constraint-solver").call(run_input={"problems": [{"title": "discounts", "problem": "price: Real in 0..500\nvip, sale: Bool\nvip => price <= 400\nsale => price >= 450\nvip and sale"},{"title": "overflow", "problem": "a, b: BitVec(8)\nugt(a, 200); ugt(b, 100)\nprove ugt(a + b, a)"},{"title": "ordering", "problem": "a, b, c: Int in 1..3\ndistinct(a, b, c)\na < b; b < c"},],})for item in client.dataset(run["defaultDatasetId"]).iterate_items():print(item["title"], item["status"], item["summary"])
Credits
- Solving: Z3 (MIT), through its
z3-solverPython package.