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.

feat(aio): attribute blob offload per event and per store outcome (#78850)

Carlos Marchalab770df332e6
diff --git a/frontend/src/taxonomy/core-filter-definitions-by-group.json b/frontend/src/taxonomy/core-filter-definitions-by-group.json
index 68425feaff8b..e8d95976564f 100644
--- a/frontend/src/taxonomy/core-filter-definitions-by-group.json
+++ b/frontend/src/taxonomy/core-filter-definitions-by-group.json
@@ -151,6 +151,22 @@
"label": "AI billable (LLM)",
"system": true
},
+ "$ai_blob_bytes": {
+ "description": "Total decoded size in bytes of the large binary payloads in this call.",
+ "examples": [
+ 245760
+ ],
+ "label": "AI binary payload size (LLM)",
+ "type": "Numeric"
+ },
+ "$ai_blob_count": {
+ "description": "Number of large binary payloads (images, audio, documents, or other base64 content) in this call.",
+ "examples": [
+ 2
+ ],
+ "label": "AI binary payload count (LLM)",
+ "type": "Numeric"
+ },
"$ai_cache_creation_1h_input_tokens": {
"description": "The number of tokens created in the 1-hour prompt cache (Anthropic only).",
"examples": [
@@ -858,10 +874,11 @@
"label": "AI Target ID (LLM)"
},
"$ai_target_type": {
- "description": "ID space of $ai_target_id. `generation_uuid` resolves against `events.uuid`; `trace_id` resolves against the `$ai_trace_id` property.",
+ "description": "ID space of $ai_target_id. `generation_uuid` resolves against `events.uuid`, `trace_id` resolves against the `$ai_trace_id` property, and `session_id` resolves against the `$ai_session_id` property.",
"examples": [
"generation_uuid",
- "trace_id"
+ "trace_id",
+ "session_id"
],
"label": "AI Target Type (LLM)"
},
@@ -4573,6 +4590,22 @@
"label": "AI billable (LLM)",
"system": true
},
+ "$ai_blob_bytes": {
+ "description": "Total decoded size in bytes of the large binary payloads in this call.",
+ "examples": [
+ 245760
+ ],
+ "label": "AI binary payload size (LLM)",
+ "type": "Numeric"
+ },
+ "$ai_blob_count": {
+ "description": "Number of large binary payloads (images, audio, documents, or other base64 content) in this call.",
+ "examples": [
+ 2
+ ],
+ "label": "AI binary payload count (LLM)",
+ "type": "Numeric"
+ },
"$ai_cache_creation_1h_input_tokens": {
"description": "The number of tokens created in the 1-hour prompt cache (Anthropic only).",
"examples": [
@@ -5280,10 +5313,11 @@
"label": "AI Target ID (LLM)"
},
"$ai_target_type": {
- "description": "ID space of $ai_target_id. `generation_uuid` resolves against `events.uuid`; `trace_id` resolves against the `$ai_trace_id` property.",
+ "description": "ID space of $ai_target_id. `generation_uuid` resolves against `events.uuid`, `trace_id` resolves against the `$ai_trace_id` property, and `session_id` resolves against the `$ai_session_id` property.",
"examples": [
"generation_uuid",
- "trace_id"
+ "trace_id",
+ "session_id"
],
"label": "AI Target Type (LLM)"
},
diff --git a/nodejs/src/ingestion/pipelines/ai/blob-offload/detect.test.ts b/nodejs/src/ingestion/pipelines/ai/blob-offload/detect.test.ts
index 2146f12bb1ed..3abcaf8821bb 100644
--- a/nodejs/src/ingestion/pipelines/ai/blob-offload/detect.test.ts
+++ b/nodejs/src/ingestion/pipelines/ai/blob-offload/detect.test.ts
@@ -16,6 +16,18 @@ const AUDIO_B64 = AUDIO_BYTES.toString('base64')
const OPTS = { minBase64Length: 8192 }
describe('extractBlobs', () => {
+ it('counts occurrences and their bytes even when the payload deduplicates to one blob', () => {
+ const url = `data:image/png;base64,${PNG_B64}`
+ const input = [
+ { role: 'user', content: [{ type: 'image_url', image_url: { url } }] },
+ { role: 'user', content: [{ type: 'image_url', image_url: { url } }] },
+ ]
+ const result = extractBlobs(input, OPTS)
+ expect(result.blobs).toHaveLength(1)
+ expect(result.occurrences).toBe(2)
+ expect(result.occurrenceBytes).toBe(2 * PNG_BYTES.length)
+ })
+
it('replaces an openai image data-URI with a pointer and captures exact bytes', () => {
const input = [
{
diff --git a/nodejs/src/ingestion/pipelines/ai/blob-offload/detect.ts b/nodejs/src/ingestion/pipelines/ai/blob-offload/detect.ts
index adc13431e6aa..1e05537d53a6 100644
--- a/nodejs/src/ingestion/pipelines/ai/blob-offload/detect.ts
+++ b/nodejs/src/ingestion/pipelines/ai/blob-offload/detect.ts
@@ -17,6 +17,10 @@ export interface ExtractionResult {
blobs: DetectedBlob[]
/** Serialized chars removed by pointer rewrites, summed per occurrence (dedup'd blobs still count each site). */
savedChars: number
+ /** Qualifying payloads counted per occurrence, so a blob repeated across turns counts each time. */
+ occurrences: number
+ /** Decoded bytes summed per occurrence, matching `occurrences`. */
+ occurrenceBytes: number
belowFloorCount: number
belowFloorBytes: number
}
@@ -31,6 +35,8 @@ const RAW_BASE64_MIME = 'application/octet-stream'
interface Extraction {
blobsByHash: Map<string, DetectedBlob>
savedChars: number
+ occurrences: number
+ occurrenceBytes: number
belowFloorCount: number
belowFloorBytes: number
minBase64Length: number
@@ -61,6 +67,8 @@ function pointerFor(
if (!state.blobsByHash.has(hash)) {
state.blobsByHash.set(hash, { bytes, mime, hash, detector })
}
+ state.occurrences += 1
+ state.occurrenceBytes += bytes.length
return encodeBlobPointer({ algo: 'sha256', hash, mime, size: bytes.length })
}
@@ -179,6 +187,8 @@ export function extractBlobs(value: unknown, opts: { minBase64Length: number }):
const state: Extraction = {
blobsByHash: new Map(),
savedChars: 0,
+ occurrences: 0,
+ occurrenceBytes: 0,
belowFloorCount: 0,
belowFloorBytes: 0,
minBase64Length: opts.minBase64Length,
@@ -188,6 +198,8 @@ export function extractBlobs(value: unknown, opts: { minBase64Length: number }):
value: rewritten,
blobs: [...state.blobsByHash.values()],
savedChars: state.savedChars,
+ occurrences: state.occurrences,
+ occurrenceBytes: state.occurrenceBytes,
belowFloorCount: state.belowFloorCount,
belowFloorBytes: state.belowFloorBytes,
}
diff --git a/nodejs/src/ingestion/pipelines/ai/metrics.ts b/nodejs/src/ingestion/pipelines/ai/metrics.ts
index dd1e92dfc716..78104e0b8ba6 100644
--- a/nodejs/src/ingestion/pipelines/ai/metrics.ts
+++ b/nodejs/src/ingestion/pipelines/ai/metrics.ts
@@ -109,7 +109,7 @@ export const aiBlobOffloadBelowFloorBytes = new Counter({
export const aiBlobOffloadBlobBytes = new Histogram({
name: 'aio_blob_offload_blob_bytes',
help: 'Decoded size of offloaded blobs',
- labelNames: ['mime_family'],
+ labelNames: ['mime_family', 'outcome'],
buckets: [1024, 8192, 65536, 262144, 1048576, 4194304, 8388608],
})
diff --git a/nodejs/src/ingestion/pipelines/ai/pipelines/steps/offload-ai-blobs-step.test.ts b/nodejs/src/ingestion/pipelines/ai/pipelines/steps/offload-ai-blobs-step.test.ts
index 7afbb3e548c8..3145a98bb0ca 100644
--- a/nodejs/src/ingestion/pipelines/ai/pipelines/steps/offload-ai-blobs-step.test.ts
+++ b/nodejs/src/ingestion/pipelines/ai/pipelines/steps/offload-ai-blobs-step.test.ts
@@ -98,6 +98,8 @@ const EMPTY_PLAN: OffloadPlan = {
blobs: [],
rewrittenProps: {},
savedChars: 0,
+ occurrenceCount: 0,
+ occurrenceBytes: 0,
belowFloorCount: 0,
belowFloorBytes: 0,
skipReason: null,
@@ -236,6 +238,10 @@ describe('offloadAiBlobs stage', () => {
const outputUrl = (props.$ai_output as ImageParts)[0].image_url.url
expect(parseBlobPointer(inputUrl)?.hash).toBe(storedHash)
expect(parseBlobPointer(outputUrl)?.hash).toBe(storedHash)
+ // Stored once, but the call carried the payload twice: the stamped
+ // properties reflect occurrences, not the deduplicated upload.
+ expect(props.$ai_blob_count).toBe(2)
+ expect(props.$ai_blob_bytes).toBe(2 * PNG_BYTES.length)
})
describe('createExtractAiBlobsStep', () => {
@@ -427,22 +433,27 @@ describe('offloadAiBlobs stage', () => {
blobs: [imageBlob, audioBlob],
rewrittenProps: { $ai_input: [{ image_url: { url: 'posthog-blob://rewritten' } }] },
savedChars: 123,
+ occurrenceCount: 3,
+ occurrenceBytes: 99,
}
const merged = mergeAiBlobPointersFanIn({ ...input, aiBlobOffloadPlan: plan }, [
{ blob: imageBlob, outcome: 'uploaded' },
{ blob: audioBlob, outcome: 'fresh' },
])
+ // Stamps the plan's per-occurrence totals, not the deduplicated upload set.
expect(merged.normalizedEvent.properties).toEqual({
$ai_input: [{ image_url: { url: 'posthog-blob://rewritten' } }],
$ai_model: 'gpt-9',
+ $ai_blob_count: 3,
+ $ai_blob_bytes: 99,
})
expect('aiBlobOffloadPlan' in merged).toBe(false)
// Original event untouched — the rewrite must not mutate in place.
expect((input.normalizedEvent.properties!.$ai_input as ImageParts)[0].image_url.url).toBe('data:original')
expect(metricsMock.aiBlobOffloadBlobsCounter.labels).toHaveBeenCalledWith('data_uri', 'image', 'uploaded')
expect(metricsMock.aiBlobOffloadBlobsCounter.labels).toHaveBeenCalledWith('data_uri', 'audio', 'fresh')
- expect(metricsMock.aiBlobOffloadBlobBytes.labels).toHaveBeenCalledWith('image')
- expect(metricsMock.aiBlobOffloadBlobBytes.labels).toHaveBeenCalledWith('audio')
+ expect(metricsMock.aiBlobOffloadBlobBytes.labels).toHaveBeenCalledWith('image', 'uploaded')
+ expect(metricsMock.aiBlobOffloadBlobBytes.labels).toHaveBeenCalledWith('audio', 'fresh')
expect(metricsMock.aiBlobOffloadBlobsPerEvent.observe).toHaveBeenCalledWith(2)
expect(metricsMock.aiBlobOffloadEventBytesSaved.observe).toHaveBeenCalledWith(123)
expect(metricsMock.aiBlobOffloadEventsCounter.labels).toHaveBeenCalledWith('offloaded')
diff --git a/nodejs/src/ingestion/pipelines/ai/pipelines/steps/offload-ai-blobs-step.ts b/nodejs/src/ingestion/pipelines/ai/pipelines/steps/offload-ai-blobs-step.ts
index 18745007b1fb..11b839a44d6e 100644
--- a/nodejs/src/ingestion/pipelines/ai/pipelines/steps/offload-ai-blobs-step.ts
+++ b/nodejs/src/ingestion/pipelines/ai/pipelines/steps/offload-ai-blobs-step.ts
@@ -62,6 +62,9 @@ interface AiBlobOffloadPlan {
/** Heavy properties rewritten with blob pointers, applied on fan-in. */
rewrittenProps: Record<string, unknown>
savedChars: number
+ /** Qualifying payloads and their decoded bytes, counted per occurrence (pre-dedup). */
+ occurrenceCount: number
+ occurrenceBytes: number
belowFloorCount: number
belowFloorBytes: number
skipReason: 'no_blobs' | 'blob_limit_exceeded' | null
@@ -100,6 +103,8 @@ export function createExtractAiBlobsStep<T extends OffloadAiBlobsInput>(
const rewrittenProps: Record<string, unknown> = {}
const blobsByHash = new Map<string, DetectedBlob>()
let savedChars = 0
+ let occurrenceCount = 0
+ let occurrenceBytes = 0
let belowFloorCount = 0
let belowFloorBytes = 0
@@ -111,6 +116,8 @@ export function createExtractAiBlobsStep<T extends OffloadAiBlobsInput>(
const extraction = extractBlobs(value, extractionOpts)
belowFloorCount += extraction.belowFloorCount
belowFloorBytes += extraction.belowFloorBytes
+ occurrenceCount += extraction.occurrences
+ occurrenceBytes += extraction.occurrenceBytes
if (extraction.blobs.length === 0) {
continue
}
@@ -125,6 +132,8 @@ export function createExtractAiBlobsStep<T extends OffloadAiBlobsInput>(
blobs: [],
rewrittenProps,
savedChars,
+ occurrenceCount,
+ occurrenceBytes,
belowFloorCount,
belowFloorBytes,
skipReason: null,
@@ -203,7 +212,7 @@ export function mergeAiBlobPointersFanIn<T extends WithAiBlobOffloadPlan<Offload
for (const { blob, outcome } of uploads) {
aiBlobOffloadBlobsCounter.labels(blob.detector, mimeFamily(blob.mime), outcome).inc()
- aiBlobOffloadBlobBytes.labels(mimeFamily(blob.mime)).observe(blob.bytes.length)
+ aiBlobOffloadBlobBytes.labels(mimeFamily(blob.mime), outcome).observe(blob.bytes.length)
}
aiBlobOffloadBlobsPerEvent.observe(uploads.length)
aiBlobOffloadEventBytesSaved.observe(plan.savedChars)
@@ -214,7 +223,12 @@ export function mergeAiBlobPointersFanIn<T extends WithAiBlobOffloadPlan<Offload
...rest,
normalizedEvent: {
...normalizedEvent,
- properties: { ...properties, ...plan.rewrittenProps },
+ properties: {
+ ...properties,
+ ...plan.rewrittenProps,
+ $ai_blob_count: plan.occurrenceCount,
+ $ai_blob_bytes: plan.occurrenceBytes,
+ },
},
}
}
diff --git a/posthog/taxonomy/taxonomy.py b/posthog/taxonomy/taxonomy.py
index 44900e598f2e..c113ea750576 100644
--- a/posthog/taxonomy/taxonomy.py
+++ b/posthog/taxonomy/taxonomy.py
@@ -2200,6 +2200,18 @@ CORE_FILTER_DEFINITIONS_BY_GROUP: dict[str, dict[str, CoreFilterDefinition]] = {
"description": "The number of tokens in the input prompt that was sent to the LLM API.",
"examples": [23],
},
+ "$ai_blob_count": {
+ "label": "AI binary payload count (LLM)",
+ "description": "Number of large binary payloads (images, audio, documents, or other base64 content) in this call.",
+ "examples": [2],
+ "type": "Numeric",
+ },
+ "$ai_blob_bytes": {
+ "label": "AI binary payload size (LLM)",
+ "description": "Total decoded size in bytes of the large binary payloads in this call.",
+ "examples": [245760],
+ "type": "Numeric",
+ },
"$ai_output_choices": {
"label": "AI output (LLM)",
"description": "The output message choices JSON that was received from the LLM API.",