Skip to content

Opt into the Strands (Python) Converse Lambda

Starting in 0.25.0, Pika ships an optional Python converse Lambda built on the Strands Agents SDK as a drop-in replacement for the default TypeScript converse Lambda. This guide walks through when to use it, how to turn it on, and what changes under the hood.

By the end of this guide, you will:

  • Understand how the Strands path differs from the TypeScript converse Lambda
  • Enable the opt-in ConverseStrandsConstruct via pika-config.ts
  • Swap your chat app's SSM lookup to route traffic through the Strands Lambda
  • Configure your local and CI bundling environment (arm64 + QEMU)
  • Verify the cutover in CloudWatch
  • Pika 0.25.0 or later
  • A working Pika deployment on AWS (both pika service stack and pika-chat stack)
  • Docker running locally for CDK Python Lambda bundling
  • On x86_64 CI runners: QEMU + buildx available (see CI/CD Bundling)

The Strands Lambda is a full-parity alternative to the TypeScript converse Lambda. It exists because the default path delegates the agent tool loop entirely to Bedrock via InvokeInlineAgentCommand, which limits what Pika can observe or override. Strands runs the loop itself, giving you:

  • Fewer LLM calls — Semantic directives are modeled as Strands Skills instead of a separate Nova Lite selection call. Typical savings of one model invocation per request
  • Iteration & budget control — Cap tool-call iterations, apply time budgets, and intercept individual tool calls
  • Prompt caching — CacheConfig(strategy="auto") on the Bedrock model (not available through InvokeInlineAgentCommand)
  • Native streaming — Real-time trace, heartbeat, and pika-metadata emission via a queue-based streaming loop

Early A/B benchmarks on representative workloads showed meaningful latency, cost, and tool-call reductions versus the TypeScript path. Actual numbers depend heavily on agent shape, tool count, and model mix — treat the Strands path as an opt-in experiment and run your own A/B before cutting production traffic over.

The Strands Lambda is deployed as a framework-managed opt-in construct. When the feature flag is on, the pika service stack instantiates a ConverseStrandsConstruct alongside the TypeScript converse Lambda — both exist simultaneously, each publishing their own SSM parameter:

  • /stack/<proj>/<stage>/function/converse_url — TypeScript Lambda (default)
  • /stack/<proj>/<stage>/function/converse_strands_url — Python/Strands Lambda

The chat app picks which URL to call by reading one of these SSM parameters in apps/pika-chat/infra/lib/stacks/pika-chat-construct.ts. Swapping between the two is a one-line change in that file.

┌──────────────────┐
│ Chat App │
│ (pika-chat) │
└────────┬─────────┘
│ reads one SSM param at deploy time
▼
/function/converse_url ◀─── ConverseFn (TypeScript, default)
/function/converse_strands_url ◀─── ConverseStrandsFunction (Python, opt-in)
  • Runtime: Python 3.14
  • Architecture: Graviton (arm64)
  • Response streaming: AWS Lambda Web Adapter layer (LambdaAdapterLayerArm64)
  • Container: FastAPI wrapper + queue-based streaming so Swarm's stream_async can coexist with Lambda Web Adapter's event loop

Edit pika-config.ts at the repository root:

export const pikaConfig: PikaConfig = {
// ...
siteFeatures: {
// ...
intentRouter: { enabled: true },
strandsConverse: { enabled: true } // ← add this
},
// ...
};

This tells pika-stack.ts to instantiate the ConverseStrandsConstruct. The TypeScript converse Lambda is always created regardless — the flag only controls whether the Strands Lambda is also deployed.

Step 2: Route Traffic to the Strands Lambda

Section titled “Step 2: Route Traffic to the Strands Lambda”

Edit apps/pika-chat/infra/lib/stacks/pika-chat-construct.ts and change the SSM parameter name that the chat app reads:

// Before (default — routes to TypeScript converse Lambda)
const converseFnUrl = ssm.StringParameter.valueForStringParameter(
this,
`/stack/${props.pikaServiceProjNameKebabCase}/${props.stage}/function/converse_url`
);
// After (routes to Strands Python Lambda)
const converseFnUrl = ssm.StringParameter.valueForStringParameter(
this,
`/stack/${props.pikaServiceProjNameKebabCase}/${props.stage}/function/converse_strands_url`
);

CDK uses Docker to build the Python Lambda for linux/arm64. On an Apple Silicon Mac, this runs natively. On Intel/AMD machines, Docker Desktop handles emulation transparently.

Verify Docker is running before cdk synth or cdk deploy:

Terminal window
docker info

Standard x86_64 GitHub Actions runners cannot execute arm64 Docker images without emulation. Add QEMU + Buildx to your deploy workflow before the CDK deploy step:

# .github/workflows/deploy.yml (or equivalent)
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: CDK deploy
run: pnpm --filter @pika/service cdk:deploy

Bundling the Python Lambda under QEMU takes roughly 60–120 seconds per build. If that becomes a bottleneck, provision native arm64 runners instead of emulating — both approaches work; this guide doesn't prescribe one.

The Strands Lambda ships with helper scripts in services/pika/package.json:

CommandWhat it does
pnpm --filter @pika/service strands:setupCreates a .venv, installs Python + dev dependencies
pnpm --filter @pika/service strands:testRuns the Python pytest suite (contract + unit tests)
pnpm --filter @pika/service strands:invokeInvokes the handler locally against real DDB + Bedrock
pnpm --filter @pika/service strands:validateFull loop: setup, test, CDK synth

Run strands:setup once per clone, then strands:test on each change. strands:invoke requires AWS credentials and an agent definition already seeded in DynamoDB.

By default each tool is invoked on its deployed Lambda. Set the LOCAL_TOOLS environment variable to send selected tools to a locally running Lambda emulator instead.

LOCAL_TOOLS accepts either inline JSON or the path of a JSON file. A value that does not parse as JSON is treated as a file path, resolved against the working directory. The JSON is an object keyed by tool id or by the function name taken from the tool's Lambda ARN. For a qualified ARN (...:function:name:alias) the function name is name, without the alias.

Each value is one of:

  • An endpoint URL string.
  • An object { "endpoint": "<url>", "function": "<name>" }. function is optional and replaces the function name derived from the ARN.
{
"my-tool-id": "http://localhost:9001",
"my-app-prod-other-tool": {
"endpoint": "http://localhost:9001",
"function": "my-app-test-other-tool"
}
}

A routed tool is invoked through a Lambda client pointed at the endpoint, addressed by bare function name rather than ARN. Tools with no matching entry stay on their deployed Lambda. Entries that are neither a URL string nor an object with a non-empty endpoint are skipped with a warning.

If the file is missing, unreadable or not valid JSON, or the JSON is not an object, LOCAL_TOOLS is ignored with a warning and every tool stays on its deployed Lambda.

LOCAL_TOOLS is re-read on every tool call, so edits to the file take effect without restarting. A transient read failure sends that one call to the deployed Lambda.

local_invoke.py (strands:invoke) uses strands-test-<epoch seconds> as the session id. Set the SESSION_ID environment variable to use a fixed session id instead, so repeated runs continue the same conversation.

Deploy the service stack first (creates the Lambda + SSM params), then the chat stack (reads the param you flipped):

Terminal window
pnpm --filter @pika/service cdk:deploy
pnpm --filter @pika/chat cdk:deploy

After deploy, verify:

  1. Lambda exists and INIT succeeds — check /aws/lambda/<project>-<stage>-ConverseStrandsFunction... in CloudWatch Logs. You should see Uvicorn running on http://0.0.0.0:8080. If you see ModuleNotFoundError, the bundling platform mismatched the runtime arch (see the caution box above).

  2. Traffic is flowing to Strands — send a chat message and tail the same log group. You should see the request handler entry and subsequent streaming traces. If you see no traffic, confirm the chat stack was deployed after flipping the SSM path.

  3. SSM parameters are correct:

Terminal window
aws ssm get-parameter --name /stack/<proj>/<stage>/function/converse_strands_url
aws ssm get-parameter --name /stack/<proj>/<stage>/function/converse_arn

These behaviors apply to every agent on the Strands path. Their settings live in custom_hooks.py (see Strands Handler Hooks).

The default model is Sonnet 5.5 (us.anthropic.claude-sonnet-5-5), used by any agent whose definition has no foundation_model. Sonnet 5.x models get adaptive thinking and a prompt-cache point after the system prompt.

When the service stack has created an application inference profile for the model, the Lambda calls Bedrock through that profile. It finds the profile ARN in the INFERENCE_PROFILE_ANTHROPIC_<modelKey> environment variables that the construct sets.

Agents with tools, knowledge bases or memory, and every swarm, don't stream tokens live. Such a turn can span several model calls, and only the last one is the answer. The answer is written once when the agent finishes, so the text the user sees and the text stored on the message are the same. Tool activity still streams as reasoning traces.

A plain agent with none of these streams tokens live. It also emits a rationale trace for each tool call it makes.

Each turn runs a circuit breaker over tool calls:

  • Identical calls: the same tool with the same input may run 3 times. Tools listed in TOOL_LOOP_POLLING_TOOLS may run 15 times.
  • Failing calls: a call that fails with the same input is retried at most once.
  • Time budget: a tool can't start unless it can finish inside the Lambda's remaining time.

The first stop for a failing call or for the deadline goes back to the model, so it can still answer. A repeat ends the turn with TOOL_LOOP_TRIPPED_MESSAGE or TOOL_DEADLINE_MESSAGE. Lambda tool invocations use a 70-second read timeout with no SDK retries.

A session is pinned to the account it was started with. The account comes from the account-id field in the session attributes (accountId / account_id, or PIKA_ACCOUNT_ID_FIELD_NAMES).

A later turn that arrives with a different account is refused with ACCOUNT_PIN_MISMATCH_MESSAGE, without calling the model or any tool. A turn with the same account is pinned to the stored value.

Apps that intentionally switch accounts within one conversation set ACCOUNT_PIN_ENABLED = False.

A collaborator entry with mode: 'tool' is exposed to the supervisor as a tool instead of joining a swarm. The user sees only the supervisor's answer.

FieldMeaning
toolNameTool name the supervisor sees
inputSchema / outputSchemaJSON Schemas; the collaborator's input and output are validated with jsonschema
timeoutSecondsWall-clock limit for one consultation (default 120)
singleSuccessToolsTools the collaborator may call successfully only once per consultation
historyRelayTO_COLLABORATOR relays the conversation to the collaborator; any other value (TO_AGENT, DISABLED) doesn't

When a collaborator fails, the supervisor stops at its next step, and the turn answers with collaborator_failure_message.

When verifyResponse is on, a correction reprompt runs only if the grade passes autoRepromptThreshold. The correction appears in the reasoning panel and is replayed to the model on later turns. It is never streamed into the answer or stored as part of it.

The reprompt is skipped when collaborators are configured, because it would re-run the supervisor and its collaborator tools after the answer has been written.

Context items the client sends (llmContextItems) are delivered in full the first time. On later turns, the model gets a short repeat marker for unchanged items. What the model actually received is stored on the message and replayed in history.

The TypeScript Lambda stays deployed while the Strands flag is on. To roll back:

  1. Change the SSM lookup in pika-chat-construct.ts back to converse_url
  2. Redeploy the chat stack

Traffic cuts back to the TypeScript path immediately. You can leave strandsConverse.enabled = true if you want to keep the Strands Lambda warm for experimentation — or flip it off to stop paying for an idle function.

Migrating from an Existing Strands Customization

Section titled “Migrating from an Existing Strands Customization”

If you previously added a Strands-style Lambda via services/pika/lib/stacks/custom-stack-defs.ts (customer override), migrate to the framework construct after upgrading to 0.25.0:

  1. Delete your custom createStrandsConverseLambda() implementation and any supporting constants from custom-stack-defs.ts. Restore the file to the pika scaffold defaults.
  2. Flip pikaConfig.siteFeatures.strandsConverse.enabled to true.
  3. Deploy.

CloudFormation will replace the old resources (deleting the customer-side logical IDs and creating new framework-side ones). The Function URL will change, but the /function/converse_strands_url SSM parameter rewrites automatically — downstream consumers picking it up through SSM will cut over on their next deploy. Expect a brief reachability gap at cutover; coordinate an off-hours deploy if needed.

  • Compare against the TypeScript path in a non-prod stage. Deploy both Lambdas side-by-side, then alternate the SSM lookup in pika-chat-construct.ts to A/B them. CloudWatch metrics (Duration, Invocations, Throttles) on the two function names give you the comparison data.
  • Review the Strands Agents SDK documentation. Customization the framework doesn't expose — tool loops, reasoning traces, custom callbacks — lives at the SDK layer. Start with strands-agents/sdk-python.
  • Understand the directive pathway. The Strands path prefers Strands Skills for directive selection (no LLM call) and falls back to the same Nova Lite filtering the TS Lambda uses when Skills aren't viable. If you use semantic directives heavily, the Instruction Augmentation guide still applies — directive authoring and DDB storage are unchanged.
  • Keep customizations out of handler.py. Constants like MAX_ITERATIONS and LAMBDA_TIMEOUT_BUFFER_SECONDS live in framework code that pika sync will overwrite. If you need to tune them, open an issue on the Pika repo so the knob becomes configuration rather than a fork of the Lambda source.