Skip to main content

Run an experiment in Python

Use the supported customer API for this Python workflow. Install httpx, then set SUBCONSCIOUS_TOKEN in your environment. Follow the authentication guide to set it up.

Check the design​

Check the design and use options that fit your decision. The example varies smartphone price, battery life, and warranty. Replace the sample levels with options that work for your decision.

US adults form the starting population here. This choice makes no claim that the respondents buy smartphones. Read Design a population for help with this choice.

Your script starts a real experiment when it sends the launch request. Check the full configuration before you run it. Use Holodeck or MCP for a guided draft and approval step.

Launch once and save the run ID​

Launch one time, and save the returned run ID.

import json
import os
import time
from pathlib import Path

import httpx

BASE_URL = "https://api.subconscious.ai"
headers = {"Authorization": f"Bearer {os.environ['SUBCONSCIOUS_TOKEN']}"}
experiment = {
"why_prompt": "How do price, battery life and warranty affect smartphone choice?",
"experiment_type": "conjoint",
"target_population": {"age": [18, 99]},
"is_private": True,
"null_levels": False,
"pre_cooked_attributes_and_levels_lookup": [
["Price", ["$649", "$849", "$1049"]],
["Battery life", ["18 hours", "24 hours", "30 hours"]],
["Warranty", ["1 year", "2 years", "3 years"]],
],
}

with httpx.Client(base_url=BASE_URL, headers=headers, timeout=300.0) as client:
launch = client.post("/api/v1/experiments", json=experiment)
launch.raise_for_status()
run = launch.json()
run_id = run["wandb_run_id"]
Path("subconscious-run.json").write_text(json.dumps(run, indent=2))
print("Keep this run ID:", run_id)

deadline = time.monotonic() + 3600
terminal = {"finished", "failed", "crashed", "killed", "lost"}
while time.monotonic() < deadline:
response = client.get(f"/api/v1/runs/{run_id}/status")
response.raise_for_status()
status = response.json()["status"]
if status in terminal:
break
time.sleep(45)
else:
raise TimeoutError(f"Resume polling {run_id}; do not launch a duplicate")

if status != "finished":
raise RuntimeError(f"Run {run_id} ended with status {status}")

response = client.get(f"/api/v1/runs/{run_id}")
response.raise_for_status()
details = response.json()["run_details"]
Path("subconscious-result.json").write_text(json.dumps(details, indent=2))

The script will not retry the launch. If the launch times out before you get an ID, check the submission first. Use Holodeck or support@subconscious.ai to check it before you launch again.

If polling times out, resume with the saved run ID. Do not run the launch block again.

Check the evidence​

Check the run and its artifacts before you treat the results as complete. A finished status alone cannot confirm that every expected result exists.

Open the returned run and its available artifacts in Analytics Studio. Or use the customer MCP tools to find and read authorized run artifacts. Run responses have no shared analytics table or fixed column set.

Read Poll a run for terminal states and missing records that you can retry. Read the methods guide to interpret the effects.