Prepare Agent for RL
Preparing an Agent for RL training is straightforward. The
side-by-side below shows a deployed Bedrock AgentCore agent and the
RL-adapted version using AgentCoreRuntimeApp.
Before vs After
Section titled “Before vs After”A math agent that uses Bedrock AgentCore Runtime and the Strands Agents framework.
from bedrock_agentcore.runtime import BedrockAgentCoreAppfrom strands import Agentfrom strands.models import BedrockModelfrom strands_tools import calculator
app = BedrockAgentCoreApp()
@app.entrypointdef invoke_agent(payload): model = BedrockModel(model_id="us.anthropic.claude-sonnet-5-5") agent = Agent( model=model, tools=[calculator], system_prompt="Solve the math problem step by step.", )
user_input = payload.get("prompt") response = agent(user_input) # Skip the leading reasoningContent block from adaptive thinking. content = response.message.get("content") or [] return "".join(block["text"] for block in content if "text" in block)
if __name__ == "__main__": app.run()The tools, system prompt, and agent logic stay the same.
from agentcore_rl_toolkit import AgentCoreRuntimeAppfrom strands import Agentfrom strands.models.openai import OpenAIModelfrom strands_tools import calculatorfrom reward import MyReward
app = AgentCoreRuntimeApp()reward_fn = MyReward()
@app.entrypointdef invoke_agent(payload: dict): # Configure the model endpoint at runtime via the invocation payload. cfg = payload["_config"] model = OpenAIModel( client_args={"api_key": cfg.get("api_key") or "EMPTY", "base_url": cfg["base_url"]}, model_id=cfg["model_id"], params=cfg.get("sampling_params", {}), ) agent = Agent( model=model, tools=[calculator], system_prompt="Solve the math problem step by step.", )
response = agent(payload["prompt"])
# Compute a scalar reward. content = response.message.get("content") or [] reward = reward_fn( response_text="".join(block["text"] for block in content if "text" in block), ground_truth=payload["answer"], ) return {"reward": float(reward)}
if __name__ == "__main__": app.run()The Reward Function
Section titled “The Reward Function”from agentcore_rl_toolkit import RewardFunction
class MyReward(RewardFunction): def __call__(self, **kwargs) -> float: # Compute a scalar from response_text, ground_truth, # test_tracker, repo_dir, or whatever context you need. return rewardDeploy to AgentCore Runtime
Section titled “Deploy to AgentCore Runtime”The container must implement the Runtime’s
HTTP contract:
serve /invocations and /ping on port 8080. AgentCoreRuntimeApp provides
these endpoints. The examples use ARM64 images built through the
Python AgentCore CLI and CodeBuild.
The Runtime must be able to reach the inference server or rollout gateway. For a private endpoint, deploy into a VPC that routes to it and allows traffic on its TCP port. Container builds also need access to dependency sources.
Each example README includes its deployment commands. For CLI setup, see installation.
A successful deploy prints the agent runtime ARN — that’s the identifier the training backend uses to invoke the agent. For verl and slime, configure the HTTP session backend:
rollout_session_backend: backend: agentcore_rl_toolkit.rollout_session.agentcore_http_session.AgentCoreHttpSession agentcore_runtime_arn: "arn:aws:bedrock-agentcore:REGION:ACCOUNT:runtime/AGENT_ID"Browse Training Backends for the toolkit’s verl, slime, SkyRL, and Tinker guides. For rLLM’s native AgentCore integration, follow the rLLM documentation.