My Python for AI 101 · Chapter 11
Deploying and tooling
Six cards on what surrounds the AgentA program that uses a language model to decide what to do (search, call a tool, answer, refuse), in a loop, with stopping rules.See in the glossary →: calling another program, reading code without running it, receiving an HTTP request, exposing a tool as an MCPModel Context Protocol: a standard for exposing tools and data sources to any agent. An MCP tool is discovered by its name, description and schema.See in the glossary → server, and measuring through the logs.
Card 53 — subprocess: calling another program
Project. Dev3Pack AI Engineering Bootcamp
In one sentence. subprocess.run launches a command (here git) and gets its output back as text.
Why it matters for an agent. The key scan reads what is about to be committed. The signer refuses to read a key stored inside a git repo.
Analogy. Asking the department next door for the statement rather than copying it by hand.
The mechanism.
done = subprocess.run(
["git", *args], capture_output=True, text=True, errors="ignore", check=True
)
out = subprocess.run(
["git", "-C", str(path.parent), "rev-parse", "--is-inside-work-tree"],
capture_output=True, text=True, timeout=10,
)
Input → output. Inside the repo → returncode = 0, stdout = 'true'. errors="ignore": a committed PNG no longer crashes the scan (the October 4 fix, reported in Gecko issue #12).
Where to read it again. my-gecko-buyer/scripts/scan_secrets.py · _git · buyer/signer.py · inside_git_repo (course) · the preflight cell, for example ch01 · cell 2 (git clone on Colab).
To reuse. Arguments as a list, never a string passed to the shell. A timeout. capture_output=True, text=True. Catch OSError and subprocess.SubprocessError.
Card 54 — subprocess and validation before deployment
Project. NovaMart Multi-Agent Support (AWS Scholars, chapter 3)
In one sentence. subprocess.run launches an outside program with a list of arguments and returns its exit code; validating every input before launching it avoids a late failure.
Why it matters for an agent. The Runtime is deployed with the agentcore CLI, driven from Python. An agentcore deploy takes 10 to 20 minutes: leaving with an empty KB ID would give a Runtime whose retrievers stay silent.
Analogy. Checking that every document of a loan file is there before sending it to the committee, rather than waiting three weeks for a RefusalAn explicit "I can't answer": no citation, zero confidence, human review. A state, not a crash.See in the glossary → over a missing page.
The mechanism.
result = subprocess.run([cli_path(), *args], cwd=PROJECT_ROOT, env=env,
capture_output=capture, text=True)
if check and result.returncode != 0:
raise RuntimeError(f"agentcore {' '.join(args)} failed (exit {result.returncode})")
missing = [key for key, value in runtime_env.items() if not value]
if missing:
raise ValueError(f"Missing runtime environment values: {', '.join(missing)}")
if not guardrail_version or str(guardrail_version).upper() == 'DRAFT':
raise ValueError("GUARDRAIL_VERSION must be a numbered version, not DRAFT")
Input → output. Replayed: {'RETURNS_KB_ID': 'ABC', 'SHIPPING_KB_ID': '', 'GUARDRAIL_VERSION': '2'} → missing = ['SHIPPING_KB_ID'] → immediate stop. shutil.which('agentcore') first checks that the CLI exists. The arguments are a list, never a concatenated string: no risk of injection through a file name.
Where to read it again. agentcore_cli.py · run · agent_orchestrator.py · deploy_to_agentcore_runtime.
To reuse. A validation function that lists every missing item at once. subprocess.run with a list, check on the exit code, capture_output when you want to show the error.
Card 55 — Reading code without running it: the ast module
Project. AI Support Agent (AWS Scholars, chapter 2)
In one sentence. setup_permissions.py reads the values of KB_ID, MEMORY_ID, REGION in main.py by parsing its text with ast.parse(), without ever importing or running that file.
Why it matters for an agent. A setup script called during development may hit a main.py that is still incomplete or contains an error; importing it would crash the setup script for a reason that has nothing to do with the permissions to setA collection with no duplicates; & gives the shared elements, <= checks that one set is contained in another.See in the glossary →.
Analogy. Reading a book’s table of contents to know what it is about, without reading it all or following its instructions.
The mechanism.
def read_settings(source):
settings = {}
for node in ast.parse(source).body:
if isinstance(node, ast.Assign):
names = [target.id for target in node.targets if isinstance(target, ast.Name)]
...
try:
settings[name] = ast.literal_eval(node.value)
except (ValueError, TypeError):
settings[name] = None
Input → output. The text of main.py (never imported) → {"KB_ID": "AFJJCJNMED", "MEMORY_ID": "CustomerSupportMemory-D0IRyF6feE", "REGION": "us-east-1"}.
Where to read it again. setup_permissions.py L21–45
To reuse. To read settings in a source file that may be untrusted or unfinished, parse it with astThe Python module that turns source code into a syntax tree you can analyse without running the code.See in the glossary → rather than importing it: there is no way to run broken or malicious code by accident.
Card 56 — HTTP entry points: the Runtime’s entrypoint and Lambda
Project. NovaMart Multi-Agent Support (AWS Scholars, chapter 3)
In one sentence. An agent server receives a request as a Dictionary (dict)A collection of values stored under keys; the shape of the trace, the receipts and the JSON answers.See in the glossary → and returns a dict: @app.entrypointThe decorator that makes a function the request handler of the AgentCore Runtime.See in the glossary → does it for AgentCore Runtime, lambda_handler(event, context) for a Lambda function.
Why it matters for an agent. This is the boundary with the outside world. The proxy Lambda reads the customer’s identity from the Cognito token, never from the request body: a customer cannot pretend to be someone else by editing the page.
Analogy. The bank counter: the ID is checked at the desk, not the name written on the slip.
The mechanism.
@app.entrypoint # POST /invocations on the Runtime
def invoke(payload, context=None):
prompt = payload.get('prompt') or payload.get('message') or ''
...
return {'result': text, 'session_id': session_id, 'trace_id': tracer.last_trace_id}
def lambda_handler(event, context): # Lambda behind a Function URL
try:
customer_id, name = _authenticated_customer(event) # from the Cognito token
except PermissionError as exc:
return _response(401, {'error': str(exc)})
...
def _claims(token: str) -> dict: # JWT payload, base64url without padding
payload = token.split('.')[1]
return json.loads(base64.urlsafe_b64decode(payload + '=' * (-len(payload) % 4)))
Input → output. Replayed: a 27-character base64url payload → -27 % 4 = 1 = sign added → {'client_id': 'abc'}. Without the padding, b64decode raises an error. A request with no Authorization header → 401 Missing bearer token; a message of more than 2,000 characters → 400.
Where to read it again. agent_orchestrator.py · run_serve · lambda_function.py · lambda_handler · lambda_function.py · _authenticated_customer · lambda_function.py · _claims.
To reuse. The identity always comes from a token validated on the server side. Business errors turned into HTTP codes (400, 401, 502). Inputs bounded (length, session_id format) before any costly call.
Card 57 — The MCP server: a function becomes a tool
Project. Dev3Pack AI Engineering Bootcamp
In one sentence. With the MCP SDK, a typed and documented Python function becomes a tool that any agent can call; mcp.tool() registers it.
Why it matters for an agent. The seven-field check becomes a service open to other agents, and it holds no key: it can judge, never pay.
Analogy. An IBAN-checking counter: you ask it whether the details are right, it does not make the transfer.
The mechanism.
mcp = MCPServer("gecko-check")
def check_purchase(intent: dict[str, Any], prepared_answer: dict[str, Any],
rpc_url: str | None = None) -> dict[str, Any]:
"""Compare a prepared purchase with the pinned intent; refuse on the first mismatch."""
...
mcp.tool()(check_purchase) # same effect as @mcp.tool() above the def
if __name__ == "__main__": # only when the file is run, not imported
mcp.run()
On the client side, async def main(), async with stdio_client(server) … and await session.call_tool(…) wait for the answer without blocking; asyncio.run(main()) starts it all.
Input → output. server/try_client.py asks about the “two bags of beans” case and receives {"passed": false, "field": "quantity", "asked": 2, "found": 1, "note": ""}.
Where to read it again. my-gecko-buyer/server/check_server.py · check_purchase and server/try_client.py (project 03) · ch12 · cells 6 to 16 (reading and sorting a server’s surface) · ch13 · cell 5 (declaring a read-only tool).
To reuse. The Type hintThe annotation of the expected type (str, list[str], str | None); Python does not enforce it, but agent frameworks and MCP read it.See in the glossary → become the input SchemaThe required shape of a piece of data: its fields, their types, their bounds.See in the glossary →, the DocstringThe text between triple quotes under a def; an agent framework or an MCP server turns it into the tool description the model reads.See in the glossary → the description. One tool, one responsibility. The URL checked before any network call. ROOT = Path(__file__).resolve().parents[1] and sys.path.insert(0, str(ROOT)) to run the script from anywhere.
Card 58 — Metrics through the logs (EMF)
Project. NovaMart Multi-Agent Support (AWS Scholars, chapter 3)
In one sentence. A JSON log in EMFEmbedded Metric Format: a JSON log from which CloudWatch extracts metrics automatically.See in the glossary → (Embedded Metric Format) is turned into metrics by CloudWatch; botocore lets you HookCode triggered at a precise moment in the agent's life, before or after a call, to enforce a rule the prompt doesn't guarantee.See in the glossary → a function onto a step of every request, here to add the header that switches that format on.
Why it matters for an agent. The Runtime’s execution role did not have the PutMetricData permission, but it could write logs. So the per-agent metrics (invocations, latency, GuardrailA filter applied to the model's inputs and outputs: harmful content, personal data, denied topics. It's tested like code, false positives included.See in the glossary → blocks) go through the logs, and feed the NovaMart-Agents dashboard.
Analogy. A well-labelled accounting entry: the general ledger is enough, and the reporting is drawn from it automatically.
The mechanism.
_metrics_logs.meta.events.register(
'before-sign.logs.PutLogEvents',
lambda request, **kwargs: request.headers.__setitem__('x-amzn-logs-format', 'json/emf'),
)
payload = {
'_aws': {'Timestamp': ..., 'CloudWatchMetrics': [{'Namespace': 'NovaMart/Agents',
'Dimensions': [['Agent']], 'Metrics': [{'Name': 'AgentLatencyMs', ...}]}]},
'Agent': agent_name, 'AgentInvocations': 1, 'AgentLatencyMs': round(latency_ms, 1),
}
try:
_metrics_logs.put_log_events(..., logEvents=[{'message': json.dumps(payload), ...}])
except Exception as exc:
logger.warning("Metric emission failed for %s: %s", agent_name, exc) # never break the reply
Input → output. In the project: the dashboard shows the invocations per agent and a peak of 5 blocks on OrchestratorAgent (the 5 adversarial attacks). The native AWS/Bedrock/Guardrails metric did not come up; only the home-made metric did.
Where to read it again. agent_orchestrator.py · botocore event · agent_orchestrator.py · _emit_agent_metrics · scripts/create_dashboard.py · main.
To reuse. One metric per agent with an Agent dimension. Emission inside a try that never breaks the answer. Check the role’s permissions before choosing the mechanism (API or logs).
Card 67 — The bounded subprocess: time, memory, CPU
Project. DRILLS.ai (Builds, OpenAI DevDay hackathon)
In one sentence. asyncio.create_subprocess_exec starts a second Python; wait_for bounds the time, resource.setrlimit bounds memory and CPU in the child, and finally kills the process if it is still running.
Why it matters for an agent. Reading an unknown PDF means running a ParserThe code that reads the model's output and rejects it if it doesn't match the schema: format, exact fields, types, bounds.See in the glossary → on untrusted data. In a separate process, an endless loop or a memory blow-up does not block the server.
Analogy. Having a suspicious parcel opened in a separate room, with a stopwatch, rather than in the middle of the office.
The mechanism.
async def extract_pdf_text(data: bytes) -> str | None:
process = None
try:
process = await asyncio.create_subprocess_exec(sys.executable, str(Path(__file__).resolve()),
stdin=asyncio.subprocess.PIPE, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.DEVNULL)
output, _ = await asyncio.wait_for(process.communicate(data), timeout=4)
...
except (OSError, ValueError, UnicodeError, asyncio.TimeoutError):
return None
finally:
if process is not None and process.returncode is None:
process.kill()
await process.wait()
# In the child:
resource.setrlimit(resource.RLIMIT_AS, (256 * 1024 * 1024, 256 * 1024 * 1024))
resource.setrlimit(resource.RLIMIT_CPU, (3, 3))
Input → output. Replayed: a one-line text PDF → the exact sentence, with no model call. b"not a pdf" or a text that is too short → None, and the AI FallbackA back-up code path run when the main call fails, to return a partial result rather than an error.See in the glossary → takes over. The test_pdf_worker_is_killed_on_timeout test checks that a child that is too slow gets killed.
Where to read it. pdf_text.py · extract_pdf_text · the child part.
To reuse. Untrusted data, separate parser, limits in the child, kill in a finally. It is not a security sandbox: it is a bound.
In the field
The chapters of my courses and my projects where these mechanisms run for real.
- Dev3Pack, Session 1: card 53.
- Dev3Pack, Session 12: card 57.
- Dev3Pack, Session 13: card 57.
- Dev3Pack, Gecko capstone: cards 53 and 57.
- AWS Scholars, chapter 2 · AI Support Agent: card 55.
- AWS Scholars, chapter 3 · NovaMart Multi-Agent Support: cards 54, 56 and 58.
- DRILLS.ai, chapter 4 · Phase 1: agents that make exercises: cards 53 and 55.
- DRILLS.ai, chapter 5 · Phase 2: the pivot to job applications: cards 53 and 67.
- DRILLS.ai, chapter 9 · MCP: three read-only tools: card 57.