Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

6.4 KB
Raw Blame

Management commands

The headless entrypoints. Four commands cover the whole product lifecycle, and together they are how autoresearch is exercised end to end locally without a browser.

Every command calls the same functions the API and the Temporal activities call. They are thin wrappers, not a parallel implementation, and they should stay that way.

All of them bypass the autoresearch feature flag. That is why local CLI testing works with no flag setup while the API and UI still need one. The real-agent path of autoresearch_train is the exception: its sandbox agent calls the flag-gated API, so the command refuses a team without the flag before it spends a run.

The four commands

  • autoresearch_train — create a pipeline and/or launch training. --pipeline-id | --team-id --target --name --horizon --create --stub --user-id --iterations --pipeline-id and --create are mutually exclusive. --user-id has no default: --create and real training require a member of the pipeline's team. --create applies the API's target rules, and an archived pipeline is refused. With --create it creates the pipeline first. With --stub it runs ../training/stub.py (deterministic, free); without it, the real Claude agent runs in a sandbox and costs roughly a dollar. Use --stub for anything that isn't specifically testing the agent.
  • autoresearch_score — load the champion, score the inference population, emit autoresearch_prediction events. --pipeline-id --user-id --dry-run --seed-fixture-bundle --prediction-date --backfill-days --prediction-date / --backfill-days backdate the emitted events, which is the only way to get matured predictions for online validation without waiting out the horizon. They are mutually exclusive; a future date or a non-positive window is refused before anything runs. --dry-run scores through the champion's real path (sandbox or recipe), applies the same prediction-date guards as a live run, and prints the distribution without emitting. --seed-fixture-bundle uploads the fixture bundle, fits it through fit_champion_model() (so it needs a sandbox provider), and only then swaps it in as champion, because scoring loads the persisted model.pkl and never fits. --user-id is the person HogQL applies access control for; without it the queries run as the pipeline's creator.
  • autoresearch_validate — pre-flight target viability, before committing to a run. --team-id --target --horizon --user-id --user-id is the person HogQL applies access control for; without it the counts run fail-closed. Note there is no --mode flag. Note also that autoresearch_train does not call this, so a target that fails here still trains.
  • autoresearch_validate_online — realized performance for predictions whose horizon has elapsed. --pipeline-id --user-id --dry-run --dry-run lists the matured dates waiting for validation and what each inference run emitted. --user-id is the person HogQL applies access control for; without it the queries run as the pipeline's creator. The command exits non-zero when any date fails, and the failed date is retried on the next pass.

One more command seeds data rather than running the lifecycle:

  • autoresearch_seed_demo — seed a fresh, learnable report_shared narrative for local end-to-end runs. --team-id --users --days --seed --direct-clickhouse --dry-run It sends identified persons (600 by default) and their events through capture, so ingestion creates the persons personhog resolves at scoring time. --direct-clickhouse writes straight to ClickHouse over Kafka instead, which leaves every prediction person-less. It also creates the action "Shared a report externally" for the action-target path, reusing one only when its steps match. It refuses to run unless DEBUG is on.

Running them locally

Prefix with CLICKHOUSE_DATABASE=posthog and run inside the flox environment:

CLICKHOUSE_DATABASE=posthog flox activate -- bash -c \
  "python manage.py autoresearch_train --create --team-id <TEAM> --target <EVENT> \
   --name '<NAME>' --horizon 30 --user-id <UID> --stub"

The env var is needed because flox only sources .env on first activation, so the setting is not otherwise present for a one-shot command.

Things that bite

  • --stub versus real is a money decision, not a speed one. A real run launches a sandbox agent and spends LLM budget. Default to stub.
  • The real-agent path needs more than an API key. It runs in a Tasks sandbox, so it needs Tasks access and a working MCP server — if the sandbox agent cannot load the autoresearch-* MCP tools it will burn a full run doing nothing and fail with "Agent recorded no iterations before the run ended."
  • Check the team id. Demo data does not reliably live on team 1 or team 2; confirm before targeting.
  • Pick a target with volume. Conversion-shaped events are rare by nature. Engagement events give far more positives, and autoresearch only trains on identified users, so raw event counts overstate what is available.
  • autoresearch_score on a fresh pipeline emits nothing if there is no champion yet — train first.
  • A backfill older than the team's drop_events_older_than is refused, because ingestion would accept and then drop the events.
  • A large --backfill-days run emits N × population events into Kafka in a tight loop and can knock over a local ingestion consumer. Prefer 10–15 days at a time.
  • Ad-hoc scripts that call sync_execute directly raise UntaggedQueryError — wrap with tag_queries(product="autoresearch", ...). These commands already tag themselves.

Where the rest of the system meets this package

  • autoresearch_train → ../training/runner.py (real) or ../training/stub.py (--stub)
  • autoresearch_score → ../inference/scoring.py
  • autoresearch_validate → ../dataset/validation.py
  • autoresearch_validate_online → ../evaluation/online_validation.py

The same functions are reached on the scheduled path via ../temporal/workflows.py, and on the HTTP path via ../presentation/views/views.py.

When editing this flow

  • Keep the commands thin. Logic belongs in the package the command calls, so the scheduled path and the API path get it too. A behavior that only exists in a command will not exist in production.
  • Adding a flag that changes modeling behavior is a smell — it means the pipeline definition should carry it instead.
  • If you add a command or change its flags, update this file to match.