01
Write the run contract before the prompt
Every run needs an owner, a deadline, a step budget, and a definition of success.
Represent the run as structured state rather than relying on a growing transcript to carry control information. Keep user and tenant identity server-side; the model may propose a tool call, but it must not choose the identity or authorization scope used to execute it. Every transition should be one of: ask the model, validate a tool request, execute an authorized tool, request approval, return a result, or stop with a classified error.
- Set independent limits for model turns, tool calls, elapsed time, and estimated spend.
- Validate tool names and JSON arguments against a server-owned schema.
- Persist a run status and correlation ID so retries do not create a second task accidentally.
The prompt proposes the next action; application code decides whether that action is valid, authorized, and still within budget.
02
Separate memory by purpose and lifetime
Memory is not ‘the whole chat forever’; it is a policy for retaining a small amount of useful state.
Keep working memory for the active thread, durable preferences for user-approved facts, and retrieval knowledge for documents that can be cited. Give each stored item an owner, source, timestamp, and deletion path. Retrieve only what the current task needs, then expose provenance to the agent so it can distinguish user-confirmed facts from retrieved text. Treat retrieved content as untrusted data, never as new system instructions.
- Scope every read and write to the authenticated user or tenant on the server.
- Do not persist credentials, private keys, or model-inferred personal facts as memory.
- Measure stale-memory corrections and retrieval precision; provide a way to forget or edit stored facts.
A useful memory is relevant, attributable, permission-scoped, and safe to delete.
03
Make tools narrow, typed, and reversible
A tool is a privileged API boundary, not a convenience function exposed to the model.
Expose the smallest capability that can complete the task. A search tool should return bounded, cited results; a write tool should accept a typed request and an idempotency key. Validate resource ownership immediately before the operation, set a timeout, and redact secrets from traces. For consequential writes, use dry-run previews or human confirmation rather than relying on a model-generated statement that the action is safe.
- Retry read-only calls only when the error is transient; make writes idempotent before retrying.
- Return stable error codes and concise, structured results rather than raw stack traces.
- Log the authorization decision, tool version, latency, outcome, and redacted argument summary.
Least privilege and observability make autonomy operable; neither can be added by a prompt alone.
A minimal run-state sketch
type AgentRunState = {
runId: string;
userId: string;
threadId: string;
remainingSteps: number;
deadlineAt: number;
completedActionIds: string[];
memoryReferences: string[];
};A design sketch, not a framework-specific API. Keep identity and permissions out of model-controlled state.
Primary sources
First-party repositories and documentation used for this chapter. Check the upstream README and release notes before implementation.
- OpenAI Agents SDK for Python — Sessions, tools, guardrails, handoffs, human-in-the-loop, tracing
- AI Agents From Scratch (pguso) — Framework-free JavaScript learning project already listed in the directory
- LangGraph — Checkpointing, durable execution, and stateful workflows