An agent is not just a language model with instructions. It is a system built from six core elements: role, focus, tools, cooperation, guardrails, and memory. This chapter breaks down each element using the contract review crew you build in this course.
A role scopes the model's perspective, priorities, and domain expertise. Without one, the model acts as a generic assistant.
Python
1risk_analyst = Agent(2 role="Commercial risk analyst",3 goal="Spot the deadlines, costs, and one-sided terms that could hurt {customer}",4 backstory=(5"You have reviewed hundreds of software agreements from the buyer's "6"side. You know which clauses cost customers money: automatic "7"renewals, price increases, short claim windows, and terms the "8"vendor can change on its own."9),10 verbose=True,11)
The role, goal, and backstory parameters prime the model to act as buyer's counsel rather than a neutral party.
Constrain each task to a single, measurable deliverable. Broad tasks produce unfocused, rambling output.
Python
1assess_risks = Task(2 description=(3"Using the key terms, write a risk memo for {customer}. Pick the five "4"terms most likely to cost them money or catch them out, and say what "5"they should do about each one."6),7 expected_output=(8"A Markdown memo of no more than 350 words: a two-sentence summary, then "9"a numbered list of five risks. Each risk has a section number, why it "10"matters, and a recommended action."11),12 agent=risk_analyst,13)
Separating drafting from verification prevents self-confirmation bias. An agent that checks its own work rarely catches its own mistakes.
Large language models cannot access local files or network services directly. Tools provide executable functions that an agent can invoke during execution.
Python
1read_file = FileReadTool()23reader = Agent(4 role="Contract analyst",5 goal="Pull every commercially important term out of a contract, with its section number",6 backstory=(7"You read contracts line by line for a procurement team. You quote "8"the contract rather than paraphrase it, and you always note the "9"section a term comes from."10),11 tools=[read_file],12 verbose=True,13)
Scope tools strictly to the agents that require them. The risk analyst operates purely on extracted text and requires no direct file access.
Guardrails enforce output accuracy through step limits, schema constraints, and secondary reviewer agents.
Python
1fact_check = Task(2 description=(3"Check every claim in the risk memo against the contract at {contract_path}. "4"Fix any number, date, or section reference that does not match the "5"contract, and remove anything the contract does not say."6),7 expected_output="The corrected memo in Markdown, followed by a 'Corrections' section that lists each change you made, or says 'None'.",8 agent=fact_checker,9)
Independent review agents catch discrepancies but can share common blind spots. Pair reviewer agents with deterministic unit tests for reliable verification.
Enable memory with memory=True when agents need state across runs, such as conversational support workflows. Leave memory disabled for stateless batch pipelines where each document requires a clean evaluation.