Production
Connect a live Python app to Cortad
Add the cortad package from PyPI to a Python app with one line where it starts, and choose whether message text is sent to Cortad.
The Python SDK is one file that uses only the standard library.
Install
Your key comes from the same setup as for Node: npx -y cortad@2.1.1 intelligence in your app's folder, or your coding agent's connect tool.
The SDK needs Python 3.10 or later.
pip install cortad
Add one line where your app starts:
import os
from cortad import cortad
cortad(key=os.environ["CORTAD_INGEST_KEY"], capture_content=True).install()
Every call your process makes through httpx or requests to the providers the Node SDK names is observed.
Choose whether message text leaves
Message text stays in your process unless you pass capture_content=True.
Say which conversation a call belongs to
When your app keeps its own conversation id, wrap each conversation in run with that id. Use this in place of the install line, so each call is observed once:
cortad_client = cortad(key=os.environ["CORTAD_INGEST_KEY"], capture_content=True)
cortad_client.install()
with cortad_client.run(conversation_id, user=customer_id):
answer = client.chat.completions.create(...)
cortad_client.outcome("resolved")
The SDK sends in batches every five seconds and when your process exits. cortad_client.flush() sends at once.
Sent as each conversation ends
Leaving run sends its conversation, waiting at most two seconds, because a serverless host freezes the process as soon as the response is out. In async code use async with, which waits without holding the event loop. If a send has taken longer than two seconds, later conversations go on without waiting until one gets through.
async def chat(conversation_id, messages):
async with cortad_client.run(conversation_id):
return await client.chat.completions.create(model="gpt-4o", messages=messages)
Hand over a reply the SDK cannot see
Inside run, a chat request to any address is captured, so Azure, Vertex, a gateway or a model on your own machine need nothing more. A reply that never goes through httpx or requests, such as Bedrock through boto3 or a stream you finish in a callback, you hand over yourself, inside run. messages is what the model was given, as {role, content}, and answer is what it said; the rest is optional.
with cortad_client.run(conversation_id):
text = call_bedrock(messages)
cortad_client.reply(messages=messages, answer=text, model="claude-sonnet-4", tokens={"in": 120, "out": 40}, ms=812)
When only some calls answer a person
Your app may also make model calls that are not replies to a person: choosing a route, rewriting a query, naming a chat. Pass only_run=True and wrap the answering call alone. Only calls inside run are then observed.
An app in another language sends one signed request after each reply.
Updated