Build an AI detective with the Reasoning Layer SDK
7 steps · 4 hours · Sorts → Terms → Inference → Negation → Fuzzy → Causal → Agents
Arrow keys to navigate · Esc for overview · F for fullscreen
1. Create an account & get your API key
Go to https://app.ovh.reasoninglayer.ai
Register → create a tenant → copy your Tenant ID and API Key
2. Clone the workshop repo & install
git clone https://gitlab.com/kortexya-pub/workshop.git
cd workshop
npm install 3. Configure your credentials in .env
REASONING_LAYER_URL=https://platform.ovh.reasoninglayer.ai/api/v1
REASONING_LAYER_TENANT_ID=your-tenant-id
REASONING_LAYER_API_KEY=your-api-key npx tsx step-01-sorts.ts You should see: Case file opened! Created 10 sorts
| Error | Fix |
|---|---|
Cannot find module | Run npm install again |
401 Unauthorized | Check your API key (no extra spaces) |
fetch failed | Check URL — needs https://, no trailing slash |
node: command not found | Install Node.js 18+ |
Last night, billionaire tech CEO Marcus Chen was found dead
in the study of his mansion during a private dinner party.
Cause of death: cyanide poisoning in his wine glass.
6 guests were present. No one has left the property.
Your mission: build an AI detective to find the killer.
| Name | Occupation | Motive | Study key? | Alibi |
|---|---|---|---|---|
| Dr. Sarah Park | Chief Scientist | $50M patent dispute | Yes | Library (unconfirmed) |
| James Chen | Nephew | $200M inheritance | Yes | Garden (says Elena there) |
| Elena Vasquez | Chef | About to be fired | Yes | Kitchen (contradicts James) |
| Prof. David Okafor | Professor | Stolen research | No | Dining room (confirmed) |
| Tom Reeves | Lawyer | Secret will change | No | Dining room (confirmed) |
| Nina Torres | Security consultant | Bitter breakup | Yes | Bathroom (no witness) |
Key idea: data, logic, and types are all the same thing — Psi-terms.
| You already know | Reasoning Layer | Key difference |
|---|---|---|
| SQL table | Sort | Multiple inheritance |
| SQL row | Psi-term | Can be incomplete (residuation) |
if/else in code | Inference rule | Rules are data — you can query them |
NULL | Uninstantiated | "I don't know yet" vs "doesn't exist" |
| Probability | Fuzzy logic | Degrees of truth, not chance |
STEP 1
Define the types of things in your investigation.
Sorts are like classes, but with multiple inheritance that actually works.
File: step-01-sorts.ts · 25 min
STEP 1
No diamond problem — the system computes GLB automatically.
GLB(suspect, witness) = person_of_interest
STEP 1
person, suspect, witness, person_of_interestevidence, physical_evidence, forensic_evidence, testimonylocation, roomcreateSort() · isSubtype() · computeGlb()
Run: npx tsx step-01-sorts.ts
STEP 2
Enter all 6 suspects, evidence, and testimonies as terms.
File: step-02-terms.ts · 20 min
STEP 2
Value.string("Alice") → {"type": "String", "value": "Alice"}
Value.integer(35) → {"type": "Integer", "value": 35}
Value.boolean(true) → {"type": "Boolean", "value": true}
Value.uninstantiated() → {"type": "Uninstantiated"} Value.uninstantiated() = "I don't know yet."
The system suspends judgment. Fundamentally different from SQL NULL.
STEP 2
Can you spot the contradictions in the testimonies?
Run: npx tsx step-02-terms.ts
We've covered knowledge representation (types + data).
Next: reasoning (rules + inference). That's where the magic happens.
STEP 3
Write logical rules. The engine deduces who could have done it.
File: step-03-inference.ts · 40 min
STEP 3
From this step on, inference uses the untagged format via psi().
TERMS (storage): Value.string("Alice") → {"type":"String","value":"Alice"}
INFERENCE (reasoning): psi('suspect', {name: "Alice"}) → "Alice"
NEVER mix them. STEP 3
alibi_contradiction — A says with B at X, but B says Yconfirmed_alibi — Two people corroborate each otherhad_opportunity — Has key to the studymatches_dna — Female + had opportunityprime_suspect — Matches DNA + has motiveThe engine chains them: prime_suspect → matches_dna → had_opportunity → facts
STEP 3
The engine works backward from your question — like a detective following leads.
STEP 3
David ↔ Tom
(both say dining room)
James claims garden with Elena,
but Elena says kitchen
Had opportunity: Sarah, James, Elena, Nina (have study keys)
DNA match: Sarah, Elena, Nina (female + study key)
Prime suspects: Sarah, Elena, Nina
Run: npx tsx step-03-inference.ts
CHALLENGE 3
Write a rule had_access_to_poison that identifies suspects who:
alibi_location = "kitchen")occupation contains "Scientist")Then query: who had access to poison AND matches DNA?
You'll need two separate rules with the same head sort —
the engine treats them as an OR. File: challenges/challenge-03-poison-access.ts
STEP 4
A detective doesn't just find the guilty —
they eliminate the innocent.
NAF (Negation as Failure): if I can't prove it, it's false.
File: step-04-negation.ts · 15 min
STEP 4
// "Find suspects who are NOT prime suspects"
nafProve({
literals: [
{ term: psi('suspect', {name: '?Name'}), negated: false },
{ term: psi('prime_suspect', {name: '?Name'}), negated: true },
]
}) James (male → no DNA match)
David & Tom (confirmed alibis)
Sarah, Elena, Nina
Run: npx tsx step-04-negation.ts
CHALLENGE 4
Write a NAF query to find suspects whose alibi
has no corroborating claim from anyone else.
Nina says she was in the bathroom, but no one filed
an alibi_claim placing her there. Use a positive literal
for "is a suspect" and a negative literal for "has a corroborating claim."
File: challenges/challenge-04-unverifiable-alibis.ts
STEP 5
Not all evidence is equally reliable.
DNA at 94% ≠ an eyewitness who "maybe saw someone."
File: step-05-fuzzy.ts · 25 min
STEP 5
DNA: Triangular(0.90, 0.94, 0.97)
Footprints: Triangular(0.4, 0.6, 0.75)
Eyewitness: Triangular(0.1, 0.3, 0.5)
STEP 5
Value.fuzzyNumber(FuzzyShape.triangular(a, b, c))fuzzyUnify() — similarity degree (0.0 – 1.0)searchTopK() — find most similar evidencefuzzyProve() — inference with truth degreesFuzzyShape uses "kind" as discriminator, not "type".
Run: npx tsx step-05-fuzzy.ts
CHALLENGE 5
Create a weighted_evidence term for the contradicting alibis:
Gaussian(0.7, 0.15)Then compare its similarity with the DNA evidence
using fuzzyUnify. Are these evidence types related?
File: challenges/challenge-05-alibi-contradiction-evidence.ts
Types, data, inference, elimination, uncertainty — done.
Next: causal reasoning — why did it happen?
STEP 6
WHY did it happen? Reconstruct the chain of events.
File: step-06-causal.ts · 20 min
STEP 6
Each level is strictly more powerful than the last.
STEP 6
rootCause('victim_dead')
Traces backward to root causes
counterfactual(...)
"What if the breakup hadn't happened?"
Run: npx tsx step-06-causal.ts
CHALLENGE 6
secret_will_change → financial_fear → murder_motiverootCause again — does the picture change?access_to_poison AND unconfirmed_alibi were both false?"File: challenges/challenge-06-extended-causality.ts
STEP 7
Your detective becomes an autonomous agent
with beliefs, goals, and intentions.
File: step-07-agents.ts · 20 min
STEP 7
STEP 7
Run: npx tsx step-07-agents.ts
| Capability | How it helped |
|---|---|
| Sorts (types) | Models people, evidence, locations with multiple inheritance |
| Terms (data) | Stores suspects, evidence, testimonies as structured data |
| Inference (rules) | Narrows 6 suspects to 3 prime suspects |
| Negation (NAF) | Clears David, Tom, and James |
| Fuzzy logic | Ranks evidence by reliability |
| Causal reasoning | Traces motive chains, tests "what if?" |
| Cognitive agents | Autonomous investigation with learning |
The evidence points to Nina Torres
But can you prove it beyond reasonable doubt?
The kitchen camera footage might change everything...
| Mystery concept | Reasoning Layer | Real-world use |
|---|---|---|
| Suspect types | Sorts (lattice) | Knowledge graph schemas |
| Case file entries | Psi-terms | Structured data + validation |
| "Who had opportunity?" | Backward chaining | Expert systems |
| "Derive everything" | Forward chaining | Materialized views |
| "Eliminate innocent" | Negation as failure | Compliance checking |
| "How reliable?" | Fuzzy logic | Anomaly detection |
| "Why did it happen?" | Causal reasoning | Root cause analysis |
| "Investigate autonomously" | BDI agents | Autonomous AI |
Resources to go further:
Key takeaway: everything is a Psi-term.
Data = logic = constraints. That's homoiconicity.