Securely Injecting Sensitive Data into Agents
Introduction & Lesson Overview
Welcome to a new course in your learning path! In the previous course, you learned how to connect your OpenAI agents to external tools and data sources. You saw how to safely manage connections and extend your agent's abilities by integrating with various tools. Now, you are ready to take on a new challenge: handling sensitive data securely within your agent workflows.
In this lesson, you will learn how to inject sensitive information—such as user names, passport numbers, or other private details—into your agent's runtime in a way that keeps this data hidden from the language model (LLM) itself. This is a crucial skill for building real-world applications, where privacy and security are top priorities. You will see how to use the context parameter in the OpenAI Agents SDK for TypeScript to manage sensitive data, ensuring that only your trusted code and tools can access it, while the LLM remains unaware of any private details.
By the end of this lesson, you will be able to securely pass sensitive data to your agent's tools and keep it out of the LLM's reach.
Understanding the Risks of Exposing Sensitive Data
Before we dive into the technical details, let's remind ourselves why handling sensitive data with care is so important. When you work with LLMs, any data you send to the model could potentially be exposed in its outputs. This means that if you pass private information—like a user's passport number or personal address—directly to the LLM, there is a risk that this data could leak out in a response, be logged, or even be accessed by someone who shouldn't see it.
These risks are not just theoretical. Data leakage can lead to privacy breaches, security vulnerabilities, and even legal trouble if you violate regulations like GDPR or CCPA. For example, if an LLM is "jailbroken" or manipulated, it might reveal information it was never supposed to share. That's why it's critical to keep sensitive data out of the LLM's input and output streams whenever possible. Instead, you want to keep this data local—only accessible to your own code and trusted tools.
Managing Sensitive Data with Context
To help you manage sensitive data securely, the OpenAI Agents SDK for TypeScript provides a context mechanism. When you run an agent, you can pass a context object that contains any data you want to make available to your tools, but this context is never sent to the LLM. Instead, it's only available to your function tools during execution.
Here's how secure context injection works in TypeScript:
-
You create your sensitive data object - This can be any TypeScript object or interface containing private information you want to keep secure.
-
You pass it to
run()as context - When you callrun(agent, input, { context: yourData }), the SDK makes your data available to tools but keeps it hidden from the LLM. -
The LLM sees your tool function description, not your data - When you define a tool like
bookHotelthat only takeshotelNameas a parameter, the LLM only sees that simplified interface. The context data is completely hidden from the LLM's view. -
The LLM calls the function normally - Based on the user's request ("book me a room at Grand Plaza Hotel"), the LLM decides to call
bookHotelwith{ hotelName: "Grand Plaza Hotel" }. It doesn't know about or need to provide the sensitive context. -
Your function receives both the LLM's parameters and your context - When your tool's
executefunction runs, it receives both the parameters from the LLM and the context as a second parameter. -
The LLM only sees the function's return value - After your function completes, the LLM receives only the return value (like "Booking confirmed for Alice Smith..."), never the sensitive data itself.
The context is passed to every tool execution during that run. This design ensures that sensitive data remains local to your application while still being accessible where needed.
