Sarghaby Araghatta
Home / Knowledge Base / Real-time data & live context
Serving & infrastructure · AI Engineering

Real-time data & live context

Fetching up-to-date information at inference — prices, inventory, a user's account state, today's news, tool results — and placing it into the prompt so the model reasons over current reality rather than frozen training data.

User requestFetch APIs/DBsFormat resultsConcatenate to contextGenerate response
Real-time data fetching process

How it works

When a user makes a request, the application immediately calls various APIs, databases, or search functions to fetch the most current information. This could include prices from an online store, inventory levels, or the latest news articles. Once the data is retrieved, the application formats it appropriately and concatenates it into the prompt that will be sent to the model. This process ensures that the model has access to real-time context, which is crucial for generating accurate and relevant responses. To manage data freshness, the system uses cache time-to-live (TTL) settings, which dictate how long data can be stored before it needs to be refreshed. In a chat application, for example, it might maintain stable prefixes that are cached but allow for a small volatile suffix that contains the latest data, ensuring that the most recent information does not invalidate the entire cache.

This approach is particularly effective because it allows the model to reason over current reality rather than relying solely on its training data, which may be outdated. For instance, if a user asks for the current price of a stock, the application fetches the latest price from a financial API and includes that in the prompt. The model then uses this fresh data to generate a response that accurately reflects the current market situation. If the data were stale or incorrect, the model's output would also be misleading, potentially leading to poor decision-making.

In addition to fetching data, the system also needs to consider the structure of the prompt. The way information is ordered and presented can significantly impact the model's understanding and the quality of the generated response. For example, placing the most relevant and recent data at the beginning of the prompt can help the model prioritize that information during generation. This careful orchestration of data retrieval and prompt construction is essential for achieving high-quality outputs.

  • User sends request for data.
  • Fetch real-time data from APIs/DBs.
  • Format the fetched results appropriately.
  • Concatenate formatted results into context.
  • Generate response using updated context.

The software-engineering analogue

This process is similar to server-side rendering, where a web application dynamically hydrates a template with live data from a database for each request. In this case, the template is the model's prompt, and the live data is the context that enhances the model's output. However, the key difference lies in the nature of the 'template engine.' Here, it is a probabilistic model with a finite context window, meaning that the amount of information we can inject is limited and costly in terms of token usage. The order and relevance of the injected data can drastically affect the correctness of the model's response and the overall efficiency of the system.

Why not the obvious alternative

A common initial approach might be to fine-tune or retrain the model with current facts to keep it up-to-date. However, this method is slow and expensive, requiring significant computational resources and time. Once the model is trained, any changes in the underlying data make the model's outputs stale almost immediately. This results in a lag between when data changes and when the model reflects those changes, which can be detrimental when accuracy is time-sensitive.

On the other hand, passing live context to the model allows for instant updates and ensures that the information is accurate for each individual request. This method is auditable, meaning you can trace back to the exact data that informed a response, providing transparency and reliability. It also eliminates the need for retraining, allowing the model to adapt to changes in real-time without the overhead of a complete training cycle.

When to use which

When dealing with volatile or user-specific facts, such as account balances, stock prices, or weather updates, it is best to use live context or tools that fetch this information on-demand. This ensures that users receive the most accurate and relevant data. For more stable domain knowledge that does not change frequently, retrieval-augmented generation (RAG) over a well-maintained index can be effective. Finally, for skills, formats, or tones that the model should consistently exhibit, fine-tuning remains a viable option, but it should be used sparingly and only when necessary.

Cache management is critical Improper cache TTL settings can lead to stale data being used, affecting response accuracy.

In production: when it breaks

The scenario Real-time data fetch failed, returning stale data for 20% of requests.
Data Source UnresponsiveData Source ResponsiveFix API LatencyFix Data FreshnessImplement ChangesRe-evaluate Fetch LogicFetch Real-time DataData Source AvailabilityAPI Latency IssuesData Freshness CheckUpdate Fetch LogicMonitor Response Times
Debugging real-time data fetch

Why it happens. Real-time data fetching can fail due to network latency or API rate limits, causing stale data to be used during inference. If the system does not handle these failures gracefully, it may fallback to outdated cached data instead of fetching the latest information.

  • Check the API response times for the data source.
  • Log the frequency of API calls and any errors returned.
  • Monitor the cache expiration settings and the age of cached data.
The call To fix, implement exponential backoff for API retries and adjust cache settings to ensure data freshness. The tradeoff is increased load on the API during high traffic periods. Monitor the average age of cached data and API error rates to catch future issues early.

What it delivers

This approach delivers answers that reflect the present moment, grounded in real-time systems of record. It provides users with accurate information without the need for retraining, ensuring that the model's responses are timely and relevant.