Access request headers¶
When your agent is deployed, you may need to access HTTP request headers for authentication, tracking, or custom metadata. DataRobot makes headers available to your agent code through the chat() function's **kwargs parameter.
Extracting X-Untrusted-* headers¶
Headers with the X-Untrusted-* prefix are passed through from the original request and are available in the kwargs dictionary. Located in agent_generic_base/custom_model/custom.py:
from typing import Iterator, Union
from openai.types.chat import CompletionCreateParams
from openai.types.chat.completion_create_params import (
CompletionCreateParamsNonStreaming,
CompletionCreateParamsStreaming,
)
def chat(
completion_create_params: CompletionCreateParams
| CompletionCreateParamsNonStreaming
| CompletionCreateParamsStreaming,
model: str,
**kwargs,
) -> Union[CustomModelChatResponse, Iterator[CustomModelStreamingResponse]]:
# Extract all headers from kwargs
headers = kwargs.get("headers", {})
# Access specific X-Untrusted-* headers
authorization_header = headers.get("X-Untrusted-Authorization")
custom_header = headers.get("X-Untrusted-Custom-Metadata")
# Use headers in your agent logic
if authorization_header:
# Pass to downstream services, tools, etc.
print(f"Authorization header: {authorization_header}")
# Continue with agent logic...
Common use cases¶
Request headers can be used for various purposes in your agent workflows. The following examples demonstrate common patterns for extracting and using header information:
Passing authentication to external services¶
Extract authentication tokens from request headers and pass them to external services. This allows you to forward authentication credentials from incoming requests to downstream tools and services:
headers = kwargs.get("headers", {})
auth_token = headers.get("X-Untrusted-Authorization")
# Use the token to authenticate with external APIs
tool_client = MyTool(auth_token=auth_token)
Tracking request metadata¶
Use headers to track request IDs, user IDs, or other metadata for debugging and analytics. This helps you trace requests through your agent workflow:
headers = kwargs.get("headers", {})
request_id = headers.get("X-Untrusted-Request-ID")
user_id = headers.get("X-Untrusted-User-ID")
# Log metadata for debugging or analytics
logging.info(f"Processing request {request_id} for user {user_id}")
Conditional agent behavior¶
Adjust agent behavior based on request context from headers. This enables you to customize agent behavior for different regions, users, or deployment contexts:
headers = kwargs.get("headers", {})
region = headers.get("X-Untrusted-Region")
# Adjust agent behavior based on request context
if region == "EU":
agent = MyAgent(enable_gdpr_mode=True)
Important considerations¶
When working with request headers in your agent code, keep the following in mind:
- Only headers with the
X-Untrusted-*prefix are passed through to your agent code. - Headers are case-sensitive when accessing them from the dictionary.
- Always provide default values or check for
Nonewhen accessing headers that may not be present. - Headers are available in both streaming and non-streaming chat responses.
Log request headers for debugging¶
Log all available headers to inspect what's being passed to your agent:
import logging
def chat(completion_create_params, model, **kwargs):
headers = kwargs.get("headers", {})
# Log all headers to inspect what's available
if headers:
logging.warning(f"All headers: {dict(headers)}")
# Continue with agent logic...