Implement agents and tools
Define bounded goals, schemas, permissions, memory, tool selection, failure handling, human approval, and observable agent execution.
- Lesson
- d2-lesson
- Practice pool
- d2-questions
- Application
- aip-l04
Implement secure model APIs, deterministic workflows, agents, tools, enterprise integrations, deployment, and production application patterns.
Define bounded goals, schemas, permissions, memory, tool selection, failure handling, human approval, and observable agent execution.
Package, configure, release, scale, secure, test, roll back, and isolate model-dependent application changes.
Connect identity, APIs, events, data systems, search, business workflows, and records with least privilege and failure isolation.
Handle invocation, streaming, quotas, retries, token limits, structured output, errors, authentication, and model-specific behavior.
Choose synchronous, asynchronous, event-driven, workflow, cache, batch, and developer-tool patterns based on constraints.
A production GenAI application is a distributed system with a probabilistic component. Keep deterministic concerns—identity, authorization, schemas, transactions, quotas, retries, state transitions, approvals, and audit—outside the model. Give the model only the context and tools required for the current task, then validate every boundary crossing.
Choose a deterministic workflow when the sequence and rules are known. Choose an agent only when planning among bounded alternatives materially improves the task. Increased autonomy expands the failure surface and must be matched by stronger tool controls, budgets, stop conditions, observation, and human oversight.
| Objective | Required judgment | Proof |
|---|---|---|
| 2.1 Implement agents and tools | Bound goals, schemas, identities, memory, tools, approvals, retries, and termination | Unauthorized or malformed tool requests are denied and every action is traceable |
| 2.2 Deploy GenAI applications | Package model-dependent inputs, isolate environments, gate releases, scale, and roll back | A failed candidate returns to the exact prior configuration |
| 2.3 Integrate enterprise systems | Connect identity, APIs, events, data, records, and workflows with least privilege and failure isolation | Integration failures do not corrupt records or widen access |
| 2.4 Use foundation model APIs | Handle authentication, invocation, streaming, limits, structured output, errors, quotas, and retries | The client degrades safely under malformed output, throttling, and provider failure |
| 2.5 Apply application patterns and development tools | Select sync, async, event, workflow, batch, cache, and developer patterns from constraints | The pattern meets latency and reliability goals without hidden coupling |
Use a supported Amazon Bedrock runtime API and understand the abstraction chosen. A common conversation interface can reduce model-specific client logic, while model-native request formats may expose different capabilities. Record the exact model identifier or inference profile, Region, API, prompt template, parameters, guardrails, and output parser as release configuration.
Design the client for token and output limits. Validate input size before invocation. Reserve space for expected output. Treat truncation, refusal, malformed structured output, throttling, access denial, model unavailability, timeout, and safety intervention as explicit states. Streaming improves perceived latency but exposes partial output; buffer or moderate where the application must not display incomplete unsafe content. Never retry a mutating business action merely because the model response timed out.
Retries need bounded exponential backoff, jitter, deadlines, and idempotency where the surrounding operation supports it. A retry storm can exhaust quotas and increase cost. Queue or shed noncritical work, route to an approved fallback, or return a clear unavailable state. Do not silently switch to a model that changes data residency, quality, safety, or contractual properties.
Define the agent's allowed goal and prohibited outcomes. Each tool needs a narrow name, description, strict input and output schema, scoped execution identity, timeout, size limit, error model, idempotency behavior, and audit event. Authorize the requested action in deterministic code using the authenticated user and current resource—not the model's claim about who the user is.
Separate read-only tools from mutating tools. Require human approval for irreversible, high-impact, financially material, externally visible, or ambiguous actions. Approval should show the exact proposed action, target, relevant evidence, and consequences. Revalidate authorization and state after approval because the world may have changed.
Treat tool output as untrusted content. A retrieved webpage, ticket, database field, or email can contain indirect prompt injection. Delimit it, validate schema and provenance, limit size, and prevent it from altering system instructions or tool permissions. Constrain loops with maximum steps, time, token, and cost budgets. Record plan, tool request, authorization decision, result, retry, denial, and final status without exposing secrets.
Memory is a data store, not intuition. Define which facts may persist, who owns them, retention, tenant partitioning, correction, deletion, and whether the model may treat them as authoritative. Conversation history can leak data or exceed context limits; summarize only with validation appropriate to the use case.
Use API Gateway, Lambda, Step Functions, EventBridge, queues, streams, containers, and managed data services according to workload needs rather than adding every service. A synchronous request fits short interactive tasks with bounded latency. Asynchronous queues fit long or bursty work and enable backpressure. Step Functions fits known orchestration with explicit state, retries, branching, and approval. EventBridge fits routed business or service events. Batch fits large offline evaluation or enrichment where interactive latency is irrelevant.
Preserve transaction boundaries. If generated content updates a system of record, validate the output, authorize the user, check current state, use idempotency, and record the authoritative transaction result. The model should propose; deterministic application logic should commit. Use correlation IDs across retrieval, model, tool, and business events.
Package application code, prompt versions, model IDs, inference parameters, tool schemas, retrieval configuration, guardrails, and evaluation thresholds. Keep environment-specific secrets and resource identifiers outside the artifact. Use infrastructure as code and separate build from deployment identities. Run unit, integration, security, evaluation, and smoke checks in an isolated environment.
Deploy progressively when impact warrants it. Compare candidate and baseline on quality, safety, latency, errors, and cost. Keep a rollback image and all configuration required to restore behavior. A model alias or prompt edit made outside the release process can invalidate reproducibility. Rollback triggers should include hard safety failures and business correctness, not only HTTP health.
| Situation | Prefer | Required control |
|---|---|---|
| Known multi-step business process | Step Functions or equivalent deterministic workflow | Explicit states, retries, compensation, and audit |
| Ambiguous planning across safe tools | Bounded agent | Tool allowlist, scoped identities, budgets, and observation |
| Irreversible action | Human approval plus deterministic execution | Show exact target and recheck authorization/state |
| Bursty long-running inference | Queue-backed asynchronous workers | Backpressure, idempotency, dead-letter handling |
| Interactive generation | Synchronous or streaming call | Deadline, partial-output policy, and safe failure |
| Release model or prompt change | Versioned deployment with evaluation gate | Exact rollback configuration |
A support agent reads tickets, searches approved knowledge, drafts replies, and may issue refunds up to a policy limit.