Generative AI and Large Language Models (LLMs) have captured the imagination of the Media & Entertainment industry. The dream is to create truly personal, dynamic, and "magical" user experiences. However, a significant gap exists between this dream and today's reality.

The problem? GenAI agents are only as good as the context they are given.

An LLM, in isolation, is like a brilliant concierge locked in a room with no windows. It can answer questions eloquently, but it has no awareness of what's happening right now. For media giants like Disney, HBO, and NBC, invoking a powerful AI agent without real-time context is not just inefficient; it's a missed opportunity. It leads to generic, "one-size-fits-all" interactions that fail to capture the user's immediate intent.

To build the next generation of high-ROI AI applications, you don't just need a good model; you need a Real-time Context Engine. This is the missing data infrastructure that bridges the gap between raw streams of user behavior and intelligent, context-aware AI agents.

This is where DeltaStream comes in.

DeltaStream: The Nervous System for Your AI Agents

DeltaStream is a serverless stream processing platform that acts as the intelligent "nervous system" for your GenAI applications. It sits between your raw data streams (clicks, views, searches from Kafka) and your AI agents, performing three critical functions:

  • Real-time Feature Engineering: It filters, enriches, and transforms noisy, low-level event streams into a high-signal, context-rich profile of each user's current session.
  • Stateful Context Building: Using simple SQL, it builds and maintains a live, stateful understanding of user behavior over time, allowing agents to have memory and act on patterns, not just single events.
  • Intelligent Triggering: It defines the precise business logic for when to activate an expensive LLM agent, ensuring you only call the model at the moment of maximum impact and with perfect context.

Let's explore how this works by building a real-world use case: The AI Concierge.

The Problem: The Thursday Night "Scroll of Despair"

It's 8:30 PM. A subscriber, Alex, opens her favorite streaming app. She's had a long week and wants to unwind, but she's faced with the classic "wall of content." She scrolls past generic recommendations, searches for something, clicks on a title, reads the synopsis, and hits "back."

This is a critical moment. Alex is showing classic signs of decision fatigue. She's an engaged user at high risk of abandoning the session. A static recommendation engine is useless here. We need an agent that understands her mood right now.

The Solution: Building an AI Concierge with DeltaStream

Our goal is to proactively intervene with a helpful, personalized suggestion at the exact moment Alex feels stuck. To do this, we'll use DeltaStream to process her real-time interactions and build the context needed to trigger our AI Concierge.

Here’s the step-by-step SQL implementation in DeltaStream:

Step 1: Ingesting the Raw Data Streams

First, we tell DeltaStream about our two sources of data: a live stream of user interactions and a changelog stream of our content metadata.

  1. -- A live stream of every click, hover, search, and play event
  2. CREATE STREAM user_interaction_stream (
  3. event_type VARCHAR,
  4. user_id VARCHAR,
  5. event_ts_long BIGINT,
  6. content_id VARCHAR,
  7. search_query VARCHAR,
  8. duration_ms INT
  9. ) WITH ('topic' = 'user_interaction_stream', 'value.format' = 'json', 'timestamp' = 'event_ts_long');
  10.  
  11. -- A changelog of our content catalog (from a database like Postgres)
  12. CREATE CHANGELOG content_metadata_cl (
  13. ts_long BIGINT,
  14. content_id VARCHAR,
  15. title VARCHAR,
  16. genres VARCHAR,
  17. PRIMARY KEY(content_id)
  18. ) WITH ('topic' = 'content_metadata', 'value.format' = 'json', 'timestamp' = 'ts_long');

Step 2: Real-time Enrichment

A raw click event is just an ID. To make it useful, we need to enrich it with metadata. We use a temporal join in DeltaStream to combine our live interaction stream with our content catalog, creating a new, enriched stream.

  1. CREATE STREAM enriched_user_interaction_stream AS
  2. SELECT
  3. uis.event_type,
  4. uis.user_id,
  5. uis.event_ts_long,
  6. cm.title,
  7. cm.genres,
  8. -- Score the event based on user intent
  9. CASE uis.event_type
  10. WHEN 'view_details' THEN 3
  11. WHEN 'hover' THEN 1
  12. ELSE 0
  13. END as weighted_interest_score
  14. FROM user_interaction_stream AS uis
  15. JOIN content_metadata_cl AS cm ON uis.content_id = cm.content_id
  16. WHERE uis.event_type IN ('view_details', 'hover');

Step 3: Building Context with Hopping Windows

This is where the magic happens. We use a HOP window to create a sliding 5-minute view of each user's behavior. This window "hops" forward every minute, giving us a continuously updated, overlapping analysis of recent activity.

This query aggregates all the enriched events within the window to understand what the user is doing and what they're interested in.

  1. CREATE CHANGELOG windowed_session_stats AS
  2. SELECT
  3. window_start,
  4. window_end,
  5. user_id,
  6. genres,
  7. SUM(weighted_interest_score) AS weighted_interest_score_sum,
  8. SUM(CASE WHEN event_type = 'view_details' THEN 1 ELSE 0 END) as abandoned_views,
  9. SUM(CASE WHEN event_type = 'play' THEN 1 ELSE 0 END) > 0 as has_played_content
  10. FROM
  11. HOP (enriched_user_interaction_stream, SIZE 5 MINUTE, ADVANCE BY 1 MINUTE)
  12. GROUP BY
  13. window_start, window_end, user_id, genres;

Step 4: Intelligent Triggering and Prompt Generation

Finally, we create one last changelog that looks at our windowed stats and decides when to act. It identifies users who have a high number of abandoned_views without having played anything. When these conditions are met, it constructs the perfect, context-rich prompt and makes it available to be sent to our LLM agent.

  1. CREATE CHANGELOG windowed_session_stats_for_prompt AS
  2. SELECT
  3. user_id,
  4. LISTAGG(genres) AS genres_list,
  5. SUM(abandoned_views) as abandoned_views_in_window,
  6. SUM(CASE WHEN has_played_content = TRUE THEN 1 ELSE 0 END) as has_played_content_count
  7. FROM
  8. TUMBLE(windowed_session_stats_stream, SIZE 1 MINUTE)
  9. GROUP BY
  10. user_id
  11. -- THE TRIGGERING LOGIC:
  12. HAVING
  13. abandoned_views_in_window > 2 AND has_played_content_count = 0;

An application layer can now subscribe to the topic backing this final changelog. When a new message appears, it calls the LLM with a perfectly crafted prompt, such as:

"User alex_4b3c seems indecisive. In the last 5 minutes, they have shown strong interest in the Comedy genre but have not played any content after viewing 3 titles. Please generate a short, friendly prompt to suggest a specific title from this genre."

The Result: A Magical User Experience

Instead of staring at a static grid, Alex sees a subtle, helpful overlay:

"Hey Alex, looks like you're in the mood for a laugh tonight. Since you like sharp wit, you might love 'Veep'. Or, if you want something brand new, how about we try the first episode of 'Somebody Somewhere'?"

The session is saved. The user is delighted. And the expensive LLM call was used with maximum efficiency and impact.

This is the future of GenAI in entertainment—not as a standalone novelty, but as an integrated, context-aware intelligence powered by a real-time engine like DeltaStream.

The runnable DeltaStream SQL code for this use case is available at https://github.com/deltastreaminc/examples.

This blog was written by the author with assistance from AI to help with outlining, drafting, or editing.