0%
Logging an Agent and Keeping Its Cost Under Control

Logging and Costs

Reconstruct a trajectory, and keep a loop from running up spend.

10-15 min

Logging an Agent and Keeping Its Cost Under Control

A production agent fails differently from an ordinary program. It doesn't crash: it arrives at a wrong result by a plausible path. A customer tells you a search returned data from another case file, and you discover that your logs contain a single line — "agent call, status 200, 14 s" — that says nothing about the dozen or so decisions made between input and output.

This is the common thread in every agent incident: the data that would let you understand what happened is produced during execution and lost at the end. Logging an agent is therefore not an operational nicety; it is the precondition for being able to fix anything. It is also the only serious way to know what the agent costs.

What to Record

A trajectory is a sequence of turns, each consisting of a model decision and possibly a tool call. The unit of logging is therefore the turn, not the request.

# Un evenement par tour. La cle est l'identifiant de trajectoire :
# c'est lui qui permet de recoller les tours d'une meme execution.
journal.info(
    "tour_agent",
    extra={
        "trajectoire_id": trajectoire.id,     # UUID genere a l'entree
        "utilisateur_id": demande.utilisateur_id,
        "tour": 3,
        "outil": "chercher_dossiers",
        "arguments": {"requete": "facture impayee", "limite": 20},
        "resultat_taille": 4821,              # octets, pas le contenu
        "duree_ms": 340,
        "jetons_entree": 2145,
        "jetons_sortie": 88,
        "motif_arret": None,
    },
)

Comments

Comments are powered by GitHub Discussions

Sign in with GitHub to join the discussion

Link copied!