# Access request headers
URL: https://docs.datarobot.com/en/docs/agentic-ai/agentic-develop/agentic-request-headers.html

> Learn how to access HTTP request headers in your deployed agents for authentication, tracking, and custom metadata.

# 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. They are available in the `kwargs` dictionary, located in `agent/agent/custom.py`:

```
# agent/agent/custom.py
import asyncio
from concurrent.futures import ThreadPoolExecutor
from typing import Any, 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,
    load_model_result: tuple[ThreadPoolExecutor, asyncio.AbstractEventLoop],
    **kwargs: Any,
) -> 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.
        # Do not log raw authorization tokens; log only non-sensitive metadata.
        request_id = headers.get("X-Untrusted-Request-ID")
        if request_id:
            print(f"Processing request {request_id} with authorization header present")
        else:
            print("Processing request with authorization header present")

    # 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:

```
# agent/agent/custom.py
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:

```
# agent/agent/custom.py
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:

```
# agent/agent/custom.py
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 None when 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:

```
# agent/agent/custom.py
import asyncio
import logging
from concurrent.futures import ThreadPoolExecutor
from typing import Any

def chat(completion_create_params, load_model_result: tuple[ThreadPoolExecutor, asyncio.AbstractEventLoop], **kwargs: Any):
    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...
```

---
