agentleFS
PostHogKnown publisherPostHog/posthog-foss/.agents/skills/building-on-capture-v1/SKILL.md

building-on-capture-v1

Skill733 starsChanged 6 days agoMITView on GitHub

How to build on capture v1, PostHog's batch event intake API (`/i/v1/analytics/events`, `/i/v1/ai/events`). Use when sending events to capture from PostHog code, changing any capture code under `rust/capture/` (legacy or v1) or the `capture_internal` helpers, adding a per-event option (`event.options`) or a property that capture or ingestion must read, changing how Node ingestion or livestream applies capture `options`, changing the v1 request, batch, header or event schema, or checking whether an SDK or internal service uses capture v1 or legacy capture (`/batch`, `/e`, `/capture`, `$process_person_profile` or `$cookieless_mode` set as properties). Covers when to add an option versus a property and why options exist, the files to change for a new option, how to compose a v1 event at a call site, internal senders, and how to tell a v1 SDK from a legacy one.

1 other project uses a version of this.

Adopted by

Most recently taken up by

The file

Show its rules and text

We split this skill into the rules it gives an agent

15 rules. About 0 are common, 10 are distinctive, and 5 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈10

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 5

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    On behalf of a customer team, from Django: use capture_internal or capture_batch_internal for analytics events, and capture_ai_internal for …

    line 28
  2. Very specific:

    From Node: follow push-capture.ts. From Rust: follow batch-import-worker/src/emit/capture.rs.

    line 30
  3. Distinctive:

    PostHog's own telemetry (team 2): use the PostHog SDK for the language, on its v1 major.

    line 31
  4. Distinctive:

    Never produce to the ingestion Kafka topics directly.

    line 32
  5. Very specific:

    Put settings in options explicitly, for example {"process_person_profile": false}.

    line 37
  6. Distinctive:

    Add an option when capture must read, act on or change the value: timestamps, partitioning, quotas, routing or a drop decision.

    line 57
  7. Distinctive:

    Add a property when only ingestion or later services read or change the value.

    line 58
  8. Distinctive:

    Every v1 SDK passes unknown option keys through unchanged, so a new option needs no SDK change.

    line 61
  9. Distinctive:

    Do not add one without agreement from the ingestion team and the SDK owners.

    line 67
  10. Distinctive:

    Every step is required. A consumer that skips its step loses the setting, and nothing fails.

    line 71
  11. Very specific:

    Only the ingestion processors that consume the events capture publishes read or change event.properties.

    line 98
  12. Distinctive:

    Metric labels have a small, fixed set of values: Never a token, a team id, a distinct id or an event name.

    line 110
  13. Distinctive:

    The v1 endpoint rejects input that legacy capture accepts: Keep these checks, and tell senders that move from legacy about them:

    line 127
  14. Distinctive:

    Keep every per-event failure a per-event result.

    line 136
  15. Very specific:

    Update the README, the sdk-specs capture spec and the public capture v1 API docs in the same change.

    line 139

≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Building on capture v1

Capture v1 is the batch intake API that the v1 SDKs and PostHog's internal senders use. Legacy capture (/batch, /e, /capture, /i/v0/e) still serves older SDKs and stays supported, but new senders use v1.

Sources of truth, in the order to read them:

  • rust/capture/src/v1/README.md: the request, response and options contract, and how capture hands events to ingestion.
  • The capture spec in PostHog/sdk-specs: what every v1 SDK does.
  • Public docs: the capture v1 API for HTTP senders, and the per-SDK migration guides (Python, Go, Rust, Node.js) for SDK users.

Send events from PostHog code

  • On behalf of a customer team, from Django: use capture_internal or capture_batch_internal for analytics events, and capture_ai_internal for $ai_* events, from posthog/api/capture.py. They implement the headers, retries and per-event results, so do not write a new HTTP client.
  • From Node: follow push-capture.ts. From Rust: follow batch-import-worker/src/emit/capture.rs.
  • PostHog's own telemetry (team 2): use the PostHog SDK for the language, on its v1 major.
  • Never produce to the ingestion Kafka topics directly. Capture owns quotas, restrictions, deduplication and routing.
  • Some internal senders and internal SDK installs still use legacy capture at the first v1 release. references/internal-senders.md lists every sender and which capture it uses. Those move to v1 soon after the release; update the list and this skill when they do. Do not move one in an unrelated PR.

Compose a v1 event

  • Put settings in options explicitly, for example {"process_person_profile": false}. Never set them as $process_person_profile, $cookieless_mode, $ignore_sent_at or $product_tour_id properties. An HTTP sender has no SDK to move those properties into options, and capture ignores $ignore_sent_at and $product_tour_id in properties.
  • Send session_id and window_id as event fields, not as $session_id and $window_id properties.
  • For events sent on a customer's behalf, set "capture_internal": true on the batch, not in properties. Capture then records 127.0.0.1 instead of the PostHog server's IP. Capture v1 ignores a capture_internal property, which is where legacy capture reads it.
  • Send $ai_* events to /i/v1/ai/events and every other event to /i/v1/analytics/events. Capture still routes $ai_* events from the analytics endpoint to the AI topic, but only for now. Do not rely on it. The AI endpoint, and an SDK's AI capture call, drop every event that is not $ai_* with misrouted_event. That includes $mcp_* events: send them to the analytics endpoint.
  • Give every event its own UUID. On a retry, resend only the events with result retry, with the same uuid, PostHog-Request-Id and created_at, PostHog-Attempt plus one, and a new PostHog-Request-Timestamp.
  • Set PostHog-Sdk-Info to <sender>/<version>. Ingestion uses it as $lib and $lib_version only when the event has no $lib.

Choose an option or a property

Why options exist. Legacy capture took settings as $ properties inside the event data. Capture forwards properties to Kafka as raw JSON, and the client controls them, so capture could not act on a setting and ingestion could not trust one. Capture v1 moves settings into a separate options object. Capture validates each option, acts on it, and publishes it beside the event in the Kafka message, outside the client-controlled data. Ingestion then writes the matching $ property, so products and queries read the same property as before.

The rule:

  • Add an option when capture must read, act on or change the value: timestamps, partitioning, quotas, routing or a drop decision. If services after capture also need the value, ingestion copies the option into a property.
  • Add a property when only ingestion or later services read or change the value. A new property needs no capture change.

Any v1 sender can put any string key with any JSON value in options, as with properties. Every v1 SDK passes unknown option keys through unchanged, so a new option needs no SDK change. The v1 SDKs move the four legacy $ properties into options under the option names (see the mapping in the capture v1 API docs and the sdk-specs capture spec). A new option has no legacy property: capture relays it in the Kafka message, and ingestion writes it into properties when later services need it.

Options are costly to add. Capture tracks known keys in an 8-bit set (OptionKeys in analytics/types.rs), every consumer of the Kafka options keeps its own list of keys, and an option name is public API. Do not add one without agreement from the ingestion team and the SDK owners.

How an option reaches the stored event

Every step is required. A consumer that skips its step loses the setting, and nothing fails.

  1. The sender sets the option. The v1 SDKs move the four legacy $ properties into options before they send. Capture never copies a value from properties into an option.
  2. Capture checks the type of each known option (RawOptions::validate). It reads values leniently, and a value it cannot read drops that event with invalid_options. Capture does not publish a key it does not know.
  3. Capture publishes the options in the outer Kafka message, beside the event, on every v1 endpoint. /i/v1/analytics/events and /i/v1/ai/events share one handler, so the AI lane carries options the same way. The legacy endpoints and the OTel endpoint publish no options, so a consumer must accept a message without them.
  4. Every consumer of capture's output checks options on every message. For an option that replaces a legacy property, it writes that property: session_id, window_id, cookieless_mode, disable_skew_correction (as $ignore_sent_at), product_tour_id and process_person_profile. Node ingestion does this in the shared parse step, CAPTURE_OPTION_OVERRIDES, which the analytics and AI pipelines both run. Livestream does it in applyCaptureOptions.
  5. A new option is either visible or internal. When a product or a query must see it, ingestion writes it into properties. When only capture and ingestion use it, for routing or as a marker, the consumer reads it from options and never writes it into properties, so it does not reach ClickHouse.

Add an option

Change all of these in one PR, or in an ordered stack where consumers ship before capture sends the key:

  1. Capture, analytics/types.rs: Options, ExpectedOption (ALL, as_str), the coercer and branch in RawOptions::validate, OutboundOptions and outbound_options, and build_property_injections while the splice code exists. Act on the value where capture needs it (for example process.rs, or has_property for a quota predicate).
  2. Capture tests: the option tests in analytics/types.rs, test_utils.rs and tests/v1_publish.rs.
  3. Consumers of the Kafka options: CAPTURE_OPTION_OVERRIDES in parse-kafka-message.ts and its test, and livestream/events/kafka.go. Services that read and re-produce capture messages must keep the options field and the legacy_property_splice header (CapturedEvent and CapturedEventHeaders in common/types/src/event.rs, used by kafka-deduplicator).
  4. Internal senders: _OPTIONS_TO_LEGACY_PROPERTY and the docstring table in posthog/api/capture.py (test: posthog/api/test/test_capture_internal.py), and OPTIONS_TO_LEGACY_PROPERTY in push-capture.ts. Only a key that replaces a legacy property needs an entry.
  5. Docs and specs: the invalid_options entry in resolving-ingestion-warnings, the options table in the v1 README, the sdk-specs capture spec, and the public capture v1 API docs. SDKs change only when an SDK sets the option itself or has a typed options class, such as posthog-kmp's PostHogEventOptions. SDK behavior changes need the capture v1 suites in the SDK test harness.

Keep capture out of event properties

Capture works with event.options and leaves event.properties alone. This applies to the legacy handlers and to the v1 handlers.

  • Capture request handlers and processing code must not deserialize event.properties, read a value from it or write a value into it.
  • The v1 endpoints (/i/v1/*) must not gain a new use of event.properties. A value capture must read, act on or change is an option: see Choose an option or a property.
  • Only the ingestion processors that consume the events capture publishes read or change event.properties.
  • The uses that capture has today are being removed, such as the legacy handlers that read $ option properties and the v1 legacy property splice. Do not copy them or add to them.

Rules for every capture change

These apply to the legacy handlers and to the v1 handlers.

  • No panic on a request path. No unwrap(), expect() or unchecked index on request data: a panic loses the whole batch and the sender gets no result.
  • No new store or external system. Capture depends on Kafka and Redis, and on S3 for AI blobs and the fallback sink. Anything more needs the ingestion team's agreement in #team-ingestion before the code merges.
  • A new call on the request path has a timeout and a stated choice between failing open and failing closed.
  • An endpoint that builds events itself applies every limit the main pipelines apply: quota, overflow, event restrictions and the AI byte limit.
  • The AI lane is the $ai_ event name prefix (is_ai_event). The analytics endpoint sends those events to the AI topic, and the AI endpoint drops any other name as misrouted_event. A new route by event name changes both endpoints, the quota check, the README and the public docs together.
  • Metric labels have a small, fixed set of values. Never a token, a team id, a distinct id or an event name.

Model a v1 endpoint on the analytics endpoint

/i/v1/analytics/events (analytics/handler.rs) is the reference for a v1 endpoint. An endpoint that is added to v1 or moved to v1 later, such as AI or replay, meets the same requirements. Its payload can differ where its data differs. The legacy endpoints and the OTel endpoint are not models for a v1 endpoint.

What the analytics endpoint has that legacy analytics capture does not. The v1 README has the full contract:

  • Required headers. Authorization: Bearer, PostHog-Sdk-Info, PostHog-Attempt, PostHog-Request-Id, PostHog-Request-Timestamp, Content-Type: application/json and User-Agent. Legacy capture reads the token from the body and requires none of them.
  • Standard compression only. Content-Encoding is gzip, deflate, br or zstd. Legacy capture also accepts lz-string and base64 bodies.
  • One batch shape with strict event fields. event, uuid, distinct_id and timestamp are JSON strings. Legacy capture treats uuid and timestamp as optional and accepts any JSON value as distinct_id.
  • Settings in options, with a type check on each one capture knows.
  • One result for each event: ok, drop, warning or retry, with a public details reason. A sender resends only the retry events.
  • Status codes with one meaning each: 400, 401, 402, 404, 413 and 415 for the request, 408 and 5xx with Retry-After, and never 429.

The v1 endpoint rejects input that legacy capture accepts. Keep these checks, and tell senders that move from legacy about them:

  • The whole request fails with 400 for a missing required header, a body that is not valid JSON, an empty or invalid batch, or an invalid uuid. It fails with 415 for another Content-Type or Content-Encoding.
  • One event drops for a missing or too long event name, a missing or too large distinct_id (legacy capture cuts it to 200 characters), a bad timestamp, properties that is not an object, or an option value capture cannot read (invalid_options).
  • A later copy of a uuid in the same batch is dropped and counted in duplicates_dropped.
  • An event that is not $ai_* drops on the AI endpoint with misrouted_event.

Change capture v1 code

  • Keep every per-event failure a per-event result. Only the cases in the README's "Response" section fail the whole request.
  • details values are public: SDKs and docs name them. Add a new one as a constant in analytics/constants.rs.
  • SDKs on old versions keep sending for years, so contract changes stay backward compatible: a new header or field is optional, and a new result or details value only adds to the old ones. Update the README, the sdk-specs capture spec and the public capture v1 API docs in the same change.
  • Watch the change with monitoring-capture-service.

Check whether an SDK uses capture v1

An SDK is on v1 when it posts to /i/v1/analytics/events, sends the PostHog-Sdk-Info and PostHog-Request-Id headers, and takes options on its capture calls. A legacy SDK posts to /batch, /e or /capture and sets settings as $ properties.

The v1 majors are:

  • posthog-python 8, posthog-go v2, posthog-rs 1.0 and posthog-node 6 (with @posthog/ai 8 and @posthog/mcp 1 on it).
  • posthog-ios 4.0, posthog-android 4.0, and posthog_flutter 6.0 on every platform except web.
  • @posthog/react-native-plugin 3.0 for the events it sends natively: crash reports, fatal $exception events and push-opened events.
  • posthog-kmp 0.11 on Android, iOS, macOS and the JVM.

posthog-js in the browser, React Native JS events, and Flutter and KMP on the web stay on legacy capture. references/sdk-implementations.md shows where each v1 SDK builds its events, the order it applies properties and options in, and how $lib is set.

Related

  • resolving-ingestion-warnings: read when a v1 drop or ingestion warning needs explaining to a customer.
  • adding-ingestion-warnings: read before adding a warning in ingestion for a new option.
  • Person processing handbook: read when a change touches process_person_profile.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.