Causal Reasoning
causal
Counterfactual Query
Explore counterfactual scenarios (Hypothetical reasoning).
This implements the third level of Pearl's Causal Ladder: "What if I HAD done X?" (exploring alternate histories)
Creates a counterfactual scenario (hypothetical) and tracks it:
- When the condition manifests in reality, record actual outcomes
- Calculate accuracy of predictions
- Learn from mismatches to improve causal understanding
Why This Matters
Learning requires counterfactual reasoning:
- "I predicted X would happen if I did Y. Did it actually happen?"
- "What would've happened if I chose differently?"
- Improves causal edge strength based on prediction accuracy
This is how agents improve via experience: predict → observe → validate.
Args: request: Condition and query current_user: Authenticated user db: Database session
Returns: Created counterfactual scenario ID
Example: ``` Condition: "If I had responded earlier" Query: {"outcome": "user_satisfaction"}
Returns:
{
"scenario_id": "cf_456",
"condition": "If I had responded earlier",
"status": "pending"
}
When reality manifests:
{
"scenario_id": "cf_456",
"condition": "If I had responded earlier",
"status": "resolved",
"predicted": {"user_satisfaction": 0.85},
"actual": {"user_satisfaction": 0.88},
"accuracy": 0.97
}
```
post/api/v1/causal/counterfactual
Request
- The document declares no server URL.
- Auth: HTTP bearer
Headers
X-API-Keystring nullable
Request body
Response
Successful Response
Changes
No recorded changes to this operation across all 2 revisions of this API.