Pass custom headers to Amazon Bedrock AgentCore Runtime
Custom headers let you pass contextual information from your application directly to your
agent code without cluttering the main request payload. This includes authentication tokens
like JWT (JSON Web Tokens, which contain user identity and authorization claims) through the
Authorization header, allowing your agent to make decisions based on who is calling it. You
can also pass custom metadata like user preferences, session identifiers, or trace context
using headers prefixed with X-Amzn-Bedrock-AgentCore-Runtime-Custom-, giving
your agent access to up to 20 pieces of runtime context that travel alongside each request.
This information can be also used in downstream systems like AgentCore Memory that you can
namespace based on those characteristics like user_id or aud in
claims like line of business.
Amazon Bedrock AgentCore Runtime lets you pass headers in a request to your agent code provided the headers match the following criteria:
-
Header name is one of the following:
-
Starts with
X-Amzn-Bedrock-AgentCore-Runtime-Custom- -
Equal to
Authorization. This is reserved for agents with OAuth inbound access to pass in the incoming JWT token to the agent code.
-
-
Header value is not greater than 4KB in size.
-
Up to 20 headers can be configured per runtime.
Topics
Step 1: Create your agent
Create an AgentCore project using the AgentCore CLI:
agentcore create --name MyHeaderAgent cd MyHeaderAgent
Update your agent's entrypoint file to access the custom headers from the request context:
import json from bedrock_agentcore import BedrockAgentCoreApp, RequestContext from strands import Agent app = BedrockAgentCoreApp() agent = Agent() @app.entrypoint def agent_invocation(payload, context: RequestContext): """Handler for agent invocation""" user_message = payload.get( "prompt", "No prompt found in input, please guide customer to create a json payload with prompt key" ) app.logger.info("invoking agent with user message: %s", payload) response = agent(user_message) # access request headers here request_headers = context.request_headers app.logger.info("Headers: %s", json.dumps(request_headers)) return response app.run()
Step 2: Configure and deploy your agent with custom headers
Configure the request header allowlist on your agent runtime so that custom headers are forwarded to your agent code at invocation time.
Step 3: Invoke your agent with custom headers
Pass custom headers when invoking your agent so that your agent code can access them through the request context.
Step 4: (Optional) Configure inbound JWT authentication
To pass the JWT token used for OAuth-based inbound access to your agent, configure
authorizerType and authorizerConfiguration in your agent
configuration.