When multiple agents act in an environment and receive a shared reward, how do you know which agent actually caused it?
βββββββββββββββ βββββββββββββββ
β Agent A β β Agent B β
β (Policy ΟA)β β (Policy ΟB)β
ββββββββ¬βββββββ ββββββββ¬βββββββ
β β
β Both act in β
β environment β
βΌ βΌ
βββββββββββββββββββββββββββββββββββ
β Environment β
βββββββββββββββββ¬ββββββββββββββββββ
β
βΌ
ββββββββββββββββ
β Team Reward β
β = +10 β
ββββββββββββββββ
β
βΌ
β WHO CAUSED THIS REWARD? β
This framework answers that question using causal interventions.
- Installation
- Quick Start
- Key Features
- How It Works
- API Reference
- Example Output
- Project Structure
- Contributing
- License
# Install from PyPI
pip install ciso-genai
# Or install from source
git clone https://github.com/harshbopaliya/CISO-GENAI-Framework.git
cd CISO-GENAI-Framework
pip install -e .from src import (
CausalCreditAssignment,
ShapleyValueEstimator,
GridWorld,
rollout,
PolicyRegistry,
random_policy
)
import numpy as np
# Setup environment and policies
env = GridWorld(grid_size=5, max_steps=10)
policies = PolicyRegistry(grid_size=5)
agent_policies = {
"agent_A": policies.get("center"), # Smart: moves to center
"agent_B": policies.get("center"), # Smart: moves to center
}
starts = {
"agent_A": np.array([0, 0]),
"agent_B": np.array([4, 4]),
}
# Run episode and get shared reward
reward, trajectory = rollout(env, starts, agent_policies)
print(f"Team Reward: {reward}")
# CREDIT ASSIGNMENT: Who caused this reward?
credit_engine = CausalCreditAssignment(baseline_policy=random_policy)
marginals = credit_engine.compute_all_marginals(env, starts, agent_policies, rollout)
for agent, contribution in marginals.items():
print(f"{agent} contributed: {contribution:+.2f}")| Feature | Description | Method |
|---|---|---|
| Marginal Contribution | How much does each agent add? | R_with_agent - R_without_agent |
| Shapley Values | Fair credit distribution | Monte-Carlo sampling over orderings |
| Synergy Detection | Do agents help or hurt each other? | R_together - sum(R_individual) |
| Counterfactual Baselines | What-if analysis | Replace agent with null/random policy |
Instead of just observing correlations, we intervene:
Traditional: "Agent A was present when reward happened" (correlation)
CISO: "If we REPLACE Agent A with a random policy,
reward drops from +10 to +3.
Therefore Agent A CAUSED +7 of the reward." (causation)
For fair distribution, we consider all possible orderings:
# Shapley value for agent_i = average marginal contribution
# across all possible orderings of agents joining the "coalition"
shapley_estimator = ShapleyValueEstimator(baseline_policy=random_policy, num_samples=100)
shapley_values = shapley_estimator.estimate(env, starts, agent_policies, rollout)
# {'agent_A': 5.2, 'agent_B': 4.8} # Fair split of rewardCheck if agents work better together:
synergy_detector = SynergyDetector(baseline_policy=random_policy)
synergy = synergy_detector.compute_synergy(env, starts, policies, rollout, ["agent_A", "agent_B"])
# synergy > 0: Agents COMPLEMENT each other
# synergy < 0: Agents INTERFERE with each other
# synergy = 0: Agents are INDEPENDENTclass CausalCreditAssignment:
def __init__(self, baseline_policy: Callable)
def marginal_contribution(self, env, starts, policy_map, target_agent, rollout_fn) -> float
"""Compute: R_full - R_without_target_agent"""
def compute_all_marginals(self, env, starts, policy_map, rollout_fn) -> Dict[str, float]
"""Compute marginal contributions for all agents"""class ShapleyValueEstimator:
def __init__(self, baseline_policy: Callable, num_samples: int = 100)
def estimate(self, env, starts, policy_map, rollout_fn) -> Dict[str, float]
"""Estimate Shapley values via Monte-Carlo sampling"""class SynergyDetector:
def __init__(self, baseline_policy: Callable)
def compute_synergy(self, env, starts, policy_map, rollout_fn, coalition) -> float
"""Synergy = R(together) - sum(R(individual))"""
def detect_all_pairwise_synergies(self, ...) -> Dict[Tuple[str, str], float]
"""Compute synergy for all agent pairs"""$ python train.py
============================================================
π CISO-GENAI: Causal Credit Assignment Demo
============================================================
π Setup:
Grid size: 5x5
Max steps: 10
Agent A starts at: [0 0] (policy: center-seeking)
Agent B starts at: [4 4] (policy: center-seeking)
============================================================
π STEP 1: Full Episode Rollout (Both Agents Active)
============================================================
Cumulative Team Reward: -12.00
============================================================
π¬ STEP 2: Marginal Contributions (Do-Intervention)
============================================================
Question: What if each agent acted randomly instead?
Results:
agent_A: +8.00
β agent_A HELPED (reward dropped by 8.00 without them)
agent_B: +6.00
β agent_B HELPED (reward dropped by 6.00 without them)
============================================================
βοΈ STEP 3: Shapley Values (Fair Credit Assignment)
============================================================
Shapley Values (fair distribution):
agent_A: +5.20
agent_B: +4.80
============================================================
π€ STEP 4: Synergy Detection
============================================================
Synergy(A, B) = +2.00
β Agents COMPLEMENT each other (better together)
β
Demo Complete!CISO-GENAI-Framework/
βββ src/
β βββ __init__.py # Package exports
β βββ causal_engine.py # CausalCreditAssignment, ShapleyValueEstimator
β βββ synergy_engine.py # SynergyDetector, InterferenceDetector
β βββ topology_engine.py # AgentClusterer, InteractionTracker
β βββ policies.py # PolicyRegistry, predefined policies
β βββ envs/
β βββ gridworld.py # GridWorld environment, rollout function
βββ configs/
β βββ ciso_default.yaml # Default configuration
βββ train.py # Main demo script
βββ demo.py # Minimal sanity test
βββ README.md
Contributions welcome! Areas to explore:
- Temporal Credit Assignment - Handle delayed rewards (action at t=0 causes reward at t=100)
- Learning Integration - Use causal credits to train agent policies
- LLM Agent Support - Apply to multi-LLM systems (Planner, Coder, Reviewer)
- Richer Environments - Beyond GridWorld
MIT License - see LICENSE
Harsh Bopaliya
- GitHub: harshbopaliya
- LinkedIn: harshbopaliya2003
- Email: bopaliyaharsh7@gmail.com
β If you find this useful, please star the repo!