My Python for AI 101 · Chapter 02
Organizing the project
Five cards on how to lay out an 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 → project: one file for several commands, a command line to test locally, configuration read from the environment, and clients created only once.
Card 1 — One file, several commands
Project. NovaMart Multi-Agent Support (AWS Scholars, chapter 3)
In one sentence. if __name__ == '__main__' runs a block only if the file is launched directly; sys.argvThe list of command-line arguments.See in the glossary → reads the command typed there, and sys.pathThe list of folders where Python looks for the modules to import.See in the glossary → tells Python where to look for the modules to import.
Why it matters for an agent. The same file serves local testing (test, chat), deployment (deploy) and the Runtime’s server (serve). So the code tested is exactly the code deployed.
Analogy. A single customer file, several counters: each one opens it at the page that concerns it.
The mechanism.
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) # starter/
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) # src/
if __name__ == '__main__':
command = sys.argv[1] if len(sys.argv) > 1 else ''
if not command and os.path.exists(os.path.join(_SRC_DIR, _RUNTIME_MARKER)):
command = 'serve' # inside the AgentCore package
if command == 'deploy':
deploy_all()
elif command == 'test':
run_test_scenarios()
Input → output. python src/agent_orchestrator.py chat → sys.argv == ['src/agent_orchestrator.py', 'chat'] → run_chat(). Imported by tests/test_agent.py, the same file runs no command: __name__Equals '__main__' when the file is launched directly, otherwise the module's name.See in the glossary → is then 'agent_orchestrator'. Inside the Runtime package, with no argument, the marker file makes it choose serve.
Where to read it again. agent_orchestrator.py · sys.path · agent_orchestrator.py · __main__.
To reuse. A single entry point with test, chat, deploy, serve subcommands. The import path computed from __file__The path of the Python file being run.See in the glossary →, never from the current folder.
Card 2 — A local CLI with argparse + asyncio.run()
Project. AI Support Agent (AWS Scholars, chapter 2)
In one sentence. The if __name__ == "__main__": block turns invoke() (a coroutine) into a command you can test locally, with argparseThe Python module that turns command-line arguments into Python objects.See in the glossary → to read the arguments and asyncio.runStarts and runs an asynchronous function (coroutine) from synchronous code.See in the glossary → to run the coroutine from synchronous code.
Why it matters for an agent. Without this bridge, the only way to test invoke() would be to deploy first, which makes every iteration costly.
Analogy. A universal remote: a single synchronous button (“press”) that triggers, behind the scenes, a mechanism that keeps running.
The mechanism.
def main():
parser = argparse.ArgumentParser()
parser.add_argument("payload", type=str)
args = parser.parse_args()
response = asyncio.run(invoke(json.loads(args.payload)))
print(response)
Input → output. uv run main.py '{"prompt": "Hello", ...}' → the invoke() coroutine runs to the end and its answer is printed in the terminal (only if main() replaces app.run(), which starts a local server and ignores the argument).
Where to read it again. main.py L408–420
To reuse. argparse + asyncio.run() is the standard pair to give a command-line interface to any asyncThe keyword that declares a coroutine: a function that can be suspended and resumed (await), to handle several requests without blocking the program.See in the glossary → function, whatever server wraps it in production.
Card 3 — Configuration and environment
Project. Dev3Pack AI Engineering Bootcamp
In one sentence. Settings come from environment variables (os.environ, a .env file); the code validates them and refuses to start if something is missing, without ever printing the key.
Why it matters for an agent. A key written in the code ends up on GitHub. A misspelled provider must fail at startup, not in the middle of a run.
Analogy. A safe whose code is not written on the door.
The mechanism.
def load_settings(env: Mapping[str, str] | None = None, dotenv_path=None) -> Settings:
if env is None:
load_dotenv(dotenv_path)
env = os.environ
provider = env.get("BOOTCAMP_PROVIDER", "fake").strip() or "fake"
if provider not in KNOWN_PROVIDERS:
raise ConfigError(f"Unknown BOOTCAMP_PROVIDER {provider!r}; expected one of {KNOWN_PROVIDERS}")
Input → output. load_settings(env={}) → Settings(provider='fake', model=None, api_key=None, base_url=None). {"BOOTCAMP_PROVIDER": "gpt-9"} → Unknown BOOTCAMP_PROVIDER 'gpt-9'; expected one of ('fake', 'ollama', 'anthropic', 'openai'). Anthropic with no key → … needs an API key in the environment; see .env.example. (The key itself is never printed.)
Where to read it again. config.py · load_settings and llm.get_client — course · ch02 · cell 22 · ch13 · cell 23 (GECKO_LIVE: the real mode is switched on explicitly) · ch14 · cell 15 (SMOKE_URL missing: nothing is called) · buyer/mcp_client.py · GECKO_MCP_URL (with a default value).
To reuse. An injectable env parameter, to test without touching the real environment. A safe default value (fake). A closed list of accepted values. Costly actions behind an explicit variable.
Card 4 — Lazy configuration
Project. NovaMart Multi-Agent Support (AWS Scholars, chapter 3)
In one sentence. load_dotenvCopies the variables of a .env file into os.environ.See in the glossary → copies the .env file into os.environ; a __getattr__At module level, the function called when a missing attribute is read; allows values computed on demand.See in the glossary → function defined at module level computes a value only when it is asked for, then keeps it in a cache.
Why it matters for an agent. Table and bucket names and model IDs change from one account to another. Read from the environment and the CloudFormation exports, they are never written in the code (rubric criterion: “Models via config.* constants only”).
Analogy. A safe at the counter: you only open it when a customer asks for what’s inside, and you keep the copy at hand for the next one.
The mechanism.
load_dotenv() # .env -> os.environ
def __getattr__(name: str): # called only for a missing attribute
if name in _LAZY:
value = _LAZY[name]() # e.g. reads a CloudFormation export
if name not in ('RETURNS_KB_ID', ..., 'GUARDRAIL_VERSION'):
globals()[name] = value # cached: next read skips __getattr__
return value
raise AttributeError(f"module 'config' has no attribute '{name}'")
Input → output. Replayed on a test module: two reads of ORDERS_TABLE → udacity-agentcore-orders twice, but a single call to the loader (loader calls: ['ORDERS_TABLE']). The KB IDs and the 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 → are not cached, so the .env is read again each time. With no stack deployed, the access raises ConfigError with the instruction “Deploy infrastructure/starter_stack.yaml first”.
Where to read it again. config.py · load_dotenv · config.py · __getattr__.
To reuse. One config.py module as the only source of settings; importing it makes no network call; values that may change during a session (an edited .env) are not cached.
Card 5 — Clients and logger at module level
Project. NovaMart Multi-Agent Support (AWS Scholars, chapter 3)
In one sentence. Code written outside any function runs only once, at the first import: that is where you create the AWS clients and the logger, then shared by every function.
Why it matters for an agent. An agent calls its tools dozens of times per request. Creating a new boto3The AWS SDK for Python. Two interfaces: client (the API's raw format) or resource (objects with Python types).See in the glossary → client on every call adds latency and pointless connections.
Analogy. The switchboard installed once for the whole floor, rather than a line pulled for each call.
The mechanism.
logger = logging.getLogger(__name__)
dynamodb = boto3.resource('dynamodb', region_name=config.AWS_REGION) # Python types
logs_client = boto3.client('logs', region_name=config.AWS_REGION) # raw API format
logger.warning("Blocked cross-customer access: session %s requested %s", bound, customer_id)
Input → output. boto3.client('dynamodb') returns {'customer_id': {'S': 'CUST-001'}}; boto3.resource('dynamodb').Table(...) returns {'customer_id': 'CUST-001'}. The project uses resource for DynamoDB, client for everything else. The logger carries the module’s name (agent_orchestrator), which can be filtered in CloudWatch.
Where to read it again. agent_orchestrator.py · logger · agent_orchestrator.py · boto3 clients.
To reuse. One client per service, created once, with the region from the config. %s in log messages rather than an f-stringA string prefixed with f where {value} is replaced; an optional format follows the colon (!r, :.2f, :08d).See in the glossary →: the text is only formatted if the log is emitted.
In the field
The chapters of my courses and my projects where these mechanisms run for real.
- Dev3Pack, Session 2: card 3.
- Dev3Pack, Session 13: card 3.
- Dev3Pack, Session 14: card 3.
- Dev3Pack, Gecko capstone: card 3.
- AWS Scholars, chapter 2 · AI Support Agent: card 2.
- AWS Scholars, chapter 3 · NovaMart Multi-Agent Support: cards 1, 4 and 5.
- DRILLS.ai, chapter 3 · Setting up and resuming without losing anything: card 3.
- DRILLS.ai, chapter 10 · Deploying and operating: card 3.