CLOUD CONCEPT

Strategy Pattern

The Strategy Pattern encapsulates interchangeable behaviors behind a common interface so an application can select an implementation at runtime without changing its calling code.

Cloud Concepts Hub →

What is Strategy Pattern

The pattern separates what an application needs to do from the implementation chosen to do it. For foundation model integrations, it enables configuration-driven selection among supported models or providers while keeping business logic stable and enforcing consistent operational and compliance controls.

The Strategy Pattern defines a family of interchangeable implementations behind a shared contract. A caller delegates an operation to the selected strategy rather than embedding implementation-specific logic throughout the application. In a generative AI application, that contract might accept messages, generation settings, and request metadata, then return generated content, usage information, and a normalized completion status. Each strategy handles a supported model integration while the business workflow remains independent of the selected implementation.

A practical cloud architecture exposes a stable inference endpoint through an API gateway. A compute component receives the request, evaluates routing policy, and selects a strategy from a registry of approved implementations. External configuration maps workload names, tenant tiers, or task types to strategy identifiers and model settings. For example, summarization requests might use a lower-cost model, while complex reasoning requests use a model that has passed a separate quality evaluation. Changing that mapping switches subsequent eligible requests without modifying application code.

On AWS, Amazon API Gateway can provide the client-facing endpoint, AWS Lambda can execute the selection and invocation logic, and AWS AppConfig can distribute routing configuration. The application must retrieve or refresh configuration at runtime; merely storing configuration externally does not make selection dynamic. Configuration should identify approved strategies, model identifiers, supported Regions, timeouts, and generation settings. Secrets belong in a secrets-management system rather than in routing documents.

The strategy interface needs explicit capability boundaries. Models differ in context limits, tool calling, structured output, image support, streaming behavior, and safety controls. A request requiring tool use must not be routed to a strategy that cannot support it. Provider-specific adapters can translate payloads and errors beneath the shared contract. Amazon Bedrock Converse provides a consistent messaging interface for supported models, but it does not make every model equally capable or cover arbitrary external provider APIs.

Configuration-driven switching has limits. Switching between already implemented, authorized, and tested strategies can avoid a code deployment. Adding a provider with a different authentication mechanism or request format usually requires a new integration, tests, and deployment. The pattern is therefore not a promise that any future model can be adopted through a configuration change alone. It also differs from simple load balancing: selecting a strategy can change behavior and capabilities, not merely the destination serving an identical implementation.

Routing policy must preserve security and compliance requirements. Filter candidates by approved provider, data residency, model access permissions, and required safeguards before optimizing for cost or latency. A fallback must satisfy the same constraints as the primary strategy. Do not silently send sensitive prompts to an unapproved provider after a timeout. Bound retries and fallback attempts because another invocation can increase cost, repeat tool side effects, or generate a materially different answer.

The main trade-offs are integration complexity, abstraction maintenance, and possible loss of provider-specific features. Validate configuration before deployment, retain a last-known-good version, and roll out changes gradually. Track model quality, latency, error rates, token usage, and estimated cost by strategy and configuration version without logging sensitive prompt content unnecessarily. Contract tests check interface compatibility, while representative evaluations determine whether a replacement model is suitable for the actual task.

Key aspects of Strategy Pattern

The pattern combines a stable interface, interchangeable implementations, and runtime selection while preserving capability, security, and operational constraints.

Stable behavioral contract

Define a common request and response interface that business workflows depend on. Keep model-specific payload construction, authentication, and error translation outside those workflows.

Runtime strategy selection

Select an approved implementation using request context and externally managed policy. Refresh configuration during execution so switching among existing integrations does not require changing or redeploying application code.

Capability-aware interchangeability

Declare requirements such as streaming, tool calling, context length, and supported input modalities. Reject incompatible strategies rather than assuming that every implementation can satisfy every request.

Policy-constrained routing and fallback

Apply provider approval, data residency, access permissions, and safety requirements before considering performance or cost. Restrict fallback to compliant alternatives and bound additional attempts.

Controlled configuration deployment

Validate strategy identifiers and model settings before releasing routing changes. Use gradual deployment, health monitoring, and rollback to limit the impact of an unsuitable model selection.

Independent testing and evaluation

Use contract tests to verify that each implementation satisfies the shared interface. Evaluate task quality separately because interface compatibility does not establish equivalent model behavior.

Cloud services for Strategy Pattern

Amazon API Gateway provides a stable entry point, while AWS Lambda selects and executes an approved model strategy. AWS AppConfig supplies routing policy and model settings that the runtime refreshes independently of code deployment. Strategies can invoke supported models through Amazon Bedrock or use separately implemented provider integrations, retrieving external credentials from AWS Secrets Manager when necessary. The application remains responsible for capability checks, compliance filtering, bounded fallback, and response normalization. Configuration deployment controls reduce routing-change risk, but a new provider protocol still requires implementation and testing before configuration can select it.

AWS Lambda image AWS Lambda

Lambda can host the strategy registry, routing logic, and model invocation implementations. Its code selects an approved strategy using runtime configuration rather than hard-coding one provider into every business workflow.

Open AWS Lambda →
AWS AppConfig image AWS AppConfig

AppConfig distributes model-selection rules and strategy settings independently of application deployments. Validators, deployment strategies, and alarm-based rollback help control changes; application code or an integration such as the AppConfig Agent must retrieve updated configuration.

Open AWS AppConfig →
Amazon API Gateway image Amazon API Gateway

API Gateway exposes a stable API while backend strategies change. It can provide authorization and throttling, but capability-aware model selection and provider-specific translation must be implemented in the backend or an appropriate integration.

Open Amazon API Gateway →
Amazon Bedrock

Bedrock provides access to foundation models, and its Converse API offers a consistent messaging interface for supported models. A strategy can select supported model identifiers while still checking differences in capabilities, inference parameters, access, and regional availability.

Open Amazon Bedrock →
AWS Secrets Manager image AWS Secrets Manager

Secrets Manager can store credentials used by strategies that invoke external model providers. Keep credential references separate from routing policy and grant each execution role access only to the secrets its approved integrations require.

Open AWS Secrets Manager →