Metadata-Version: 2.5
Name: aeko
Version: 3.8.2
Summary: Intelligent core of the Aether ecosystem: multi-agent system for GHG inventory analysis, improvement plans and conversational support.
Project-URL: Homepage, https://github.com/AetherGases/aeko-sdk
Project-URL: Repository, https://github.com/AetherGases/aeko-sdk
Author: Eduardo Costa Amex Macal
License-Expression: MIT
License-File: LICENSE
Keywords: agents,esg,ghg,langchain,langgraph,sustainability
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Typing :: Typed
Requires-Python: <3.12,>=3.11
Requires-Dist: langchain-classic<2,>=1.0.8
Requires-Dist: langchain-core<2,>=1.6.1
Requires-Dist: langchain-google-genai<5,>=4.3.7
Requires-Dist: langchain<2,>=1.0
Requires-Dist: langgraph<2,>=1.2.11
Requires-Dist: pydantic<3,>=2.9
Description-Content-Type: text/markdown

# aeko

**The intelligent core of the Aether ecosystem** — a multi-agent system that reads a
company's greenhouse gas (GHG) inventory, explains it, and turns it into a concrete
improvement plan.

Aether helps industrial companies cut their greenhouse gas emissions. Their employees
fill in the company's GHG inventory; Aeko is what reads that inventory and answers with
pollutant analysis, green gas alternatives and a prioritized improvement plan — plus a
chatbot for the day-to-day ESG questions that come up along the way.

This package is the reusable engine behind that product. It is a **library, not a
service**: it exposes a small facade over a LangChain/LangGraph agent graph so any
backend — an API, a worker, a notebook — can embed it.

> 🇧🇷 Um resumo em português está no final deste documento: [Resumo em português](#resumo-em-português).

---

## What's new in 3.8

Version 3.8 adds **recommended gas substitutions** to the improvement plan, then splits
the work of the two reviewers. The only API change is additive.

| Area | 3.7 | 3.8 |
| --- | --- | --- |
| `AekoImprovementPlan` (3.8.0) | `defined_problem`, `method`, `reasoning` | Also `recommended_gases`: 0 to 2 `AekoRecommendedGas` (`scope` 1 to 3, `emission` in tCO2e, `reduction_percentage` 0 to 100, `name`), floats rounded to 2 decimals |
| Plan sections (3.8.0) | Three headings | Four: `## Gases recomendados` is a JSON array between `## Método` and `## Raciocínio`. `[]` is a valid answer; a missing or invalid list goes through the plan-format retries |
| Docs (3.8.1) | — | The `Gases recomendados` section and `AekoRecommendedGas` documented |
| `recommended_gases[].name` (3.8.2) | Free-form name | Standard name plus the chemical formula, as `Name (formula)` |
| `Guardrail de Saída` (3.8.2) | Also checked the draft against the analyses | A policy filter only: domain, internal leaks, injected instructions, sensitive data and tone. Code fences and agent names are rejected without calling the model |
| `Verificador de Resposta` (3.8.2) | Rejected anything absent from the analyses | Also accepts the question's own data, math derived from it, technical constants and public references, and recomputes that math |

---

## What's new in 3.7

Version 3.7 makes chat runs **safe to reject**: nothing is written and no personal data
reaches a model before the answer is approved. No signature changed.

| Area | 3.6 | 3.7 |
| --- | --- | --- |
| Writing tools (3.7.0) | Wrote as soon as an agent called them, even in a turn later rejected | Staged during the run and committed by the new `Consolidador` node, only after `Verificador de Resposta` approves (or after the report flow's coordinator). Retry-cap endings commit nothing |
| A write that fails (3.7.0) | Not retried | Retried once; if it still fails, the tool's `metadata["write_failure_message"]` is appended to the chat answer. The report is never altered |
| Personal data (3.7.1) | Sent to the models as typed | The new `Guardrail de Entrada` masks CPF, CNPJ, card and Brazilian phone numbers as placeholders such as `[CPF_1]`; the `Consolidador` restores them in the answer and in staged write arguments |
| Reviewers (3.7.1) | Configured temperature | Temperature 0, so the same draft always gets the same verdict |
| User-facing prompts (3.7.1) | Some used dashes and could expose the agents | Never name agents or the routing, and contain no em or en dash |

---

## What's new in 3.6

Version 3.6 makes `analyze()` **recover the structured inventory** instead of failing on
the first malformed JSON block. There was no 3.6.0 release; the line starts at 3.6.1.

| Area | 3.5 | 3.6 |
| --- | --- | --- |
| Inventory JSON (3.6.1) | A missing or invalid block raised `MalformedAgentOutputError` | Salvaged (the ` ```inventory ` fence, else a single object after `Raciocínio`), canonicalized, validated against the catalogs, then filled from the schema up to `INVENTORY_SCHEMA_MAX_RETRIES` (2) times. The plan stays frozen; the final error is still `MalformedAgentOutputError` |
| Tool errors (3.6.2) | A tool raising `ToolException` aborted the request | The exception becomes an observation the model can recover from |

---

## What's new in 3.5

Version 3.5 makes `analyze()` return the **inventory aligned with Postgres** next to the
plan. This one is breaking for `analyze()` callers.

| Area | 3.4 | 3.5 |
| --- | --- | --- |
| `analyze()` arguments (3.5.0) | The inventory and its ids | Also `gases`, `scopes` and `categories`, keyword-only: the catalogs whose ids the payload reuses as foreign keys |
| `analyze()` result (3.5.0) | Plan and metrics | `AekoAnalysisResponse.inventory`: an `AekoExtractedInventory` (description, period and `AekoInventoryEmission` lines, `is_reduction` telling emission from reduction rows) |
| New public DTOs (3.5.0) | — | `AekoCatalogItem`, `AekoCategoryCatalogItem`, `AekoExtractedInventory`, `AekoInventoryCatalogs`, `AekoInventoryEmission` |
| Routing marker (3.5.1) | Text after `Next agent:` could be read as the agent name | Only the last marker line is the handoff; a payload below it is kept, not misread |

---

## What's new in 3.4

Version 3.4 adds **conversation summaries**, outside the agent graph.

| Area | 3.3 | 3.4 |
| --- | --- | --- |
| `AekoMessenger.generate_summary(messages, *, id_request)` | — | Condenses the turns it is given, oldest first, without reading or updating a session |
| `AekoSummaryResponse` | — | `summary`, ready for `user_memory`, plus `aeko_metrics` |

---

## What's new in 3.3

Version 3.3 makes **sampling configurable** and adds CI. No breaking change.

| Area | 3.2 | 3.3 |
| --- | --- | --- |
| `Aeko.config()` | Fixed sampling | `temperature`, `top_p` and `top_k`, defaulting to `1.0`, `0.95` and `64` |
| CI | Publish on release only | Also `python -m pytest` on every push and pull request |

---

## What's new in 3.2

Version 3.2 adds a **ninth agent, `Verificador de Resposta`**, as the last step of the
conversational flow. It compares what the user asked with what was actually generated and
refuses an answer that claims anything the analyses do not support — a judge against
hallucination, sitting after the output guardrail rather than in place of it.

| Area | 3.1 | 3.2 |
| --- | --- | --- |
| Conversational flow | Ends at the output guardrail | Ends at `Verificador de Resposta`, which reviews what the guardrail approved |
| Delivery | The guardrail's approval delivered the draft | Only the response checker's approval delivers it |
| Retry cap | Guardrail: 3 rejections | Both reviewers: **2 rejections each** (`GUARD_RAIL_MAX_RETRIES`, `RESPONSE_CHECK_MAX_RETRIES`) |
| A turn nobody approved | Returned normally with `message.output == ""` | Raises `MalformedAgentOutputError`, carrying the run's `aeko_metrics` |
| Report flow | — | Unchanged: `analyze()` never reaches either reviewer |

The one breaking change is the last row: a rejected turn now raises instead of returning
an empty answer. Code that already handles `MalformedAgentOutputError` from `analyze()`
needs no new exception type, only the same `except` around `send_message()`.

---

## What's new in 3.1

Version 3.1 makes the SDK **report what a request cost**. Both entry points now take an
`id_request` and hand back an `AekoMetrics` alongside the answer, so the API can persist
per-request telemetry it previously had no way to obtain. Two changes are breaking.

| Area | 2.x | 3.1 |
| --- | --- | --- |
| `send_message()` | `send_message(message, session)` | `send_message(message, session, *, id_request)` — **required, keyword-only** |
| `analyze()` | `analyze(inventory, *, id_external_inventory) -> AekoImprovementPlan` | `analyze(inventory, *, id_external_inventory, id_request) -> AekoAnalysisResponse` |
| Telemetry | Logs only, readable by a person | `AekoMetrics` on every response, persistable by a machine |
| Per-agent cost | Nothing — one total per run | Tokens, model and tools called, per agent invocation |
| Failed requests | The exception, and nothing else | The exception, carrying the run's `aeko_metrics` |
| `AekoMessage` | `input`, `output`, `submitted_at`, `llm`, `input_tokens`, `output_tokens` | `input`, `output`, `submitted_at` — the cost moved to `aeko_metrics` |

The logs are unchanged: this adds a second rendering of what a run already observed, it
does not replace the stream you read in a terminal. See
[Event tracking](#5-event-tracking) for the object and
[Migrating from 2.x](#migrating-from-2x) for what to change at your call sites.

---

## What's new in 2.0

Version 2 reshapes the boundary between the SDK and the API that consumes it. The agent
system is the same; **how you hand work to it changed, and every change is breaking.**

| Area | 1.x | 2.0 |
| --- | --- | --- |
| Data objects | Dataclasses describing a call (`SessionInfo`, `MessageResponse`, …) | Pydantic models mirroring the MongoDB collections, field for field |
| Session handling | `prepare(session_id, user_info, history)`, then a process-wide session cache | No `prepare()`: the `AekoSession` document travels with every `send_message()` call |
| Chat result | One `answer` string, always populated | `AekoMessageResponse` — the turn to persist, plus the run's metadata and token cost |
| Inventory result | Prose the caller had to split apart | `AekoImprovementPlan`, parsed from four fixed sections, with retry |
| User context | Nothing | `AekoUser.role`/`usecase` and every `AekoUserMemory` reach every agent |
| Configuration | Several caches, each invalidated by hand | One runtime; writing any setting drops the agents on its own |

If you are coming from 1.x, read [Migrating from 1.x](#migrating-from-1x) — it maps every
removed name to its replacement.

---

## Table of contents

- [What the SDK does](#what-the-sdk-does)
- [The agent system](#the-agent-system)
- [Requirements](#requirements)
- [Installation](#installation)
- [Quickstart](#quickstart)
- [Integrating into other systems](#integrating-into-other-systems)
  - [Design rules that shape the integration](#design-rules-that-shape-the-integration)
  - [1. Configure once, at startup](#1-configure-once-at-startup)
  - [2. Register your tools](#2-register-your-tools)
  - [3. The conversational flow](#3-the-conversational-flow)
  - [4. The inventory flow](#4-the-inventory-flow)
  - [5. Event tracking](#5-event-tracking)
  - [6. Full example: a stateless FastAPI service](#6-full-example-a-stateless-fastapi-service)
  - [7. Error handling](#7-error-handling)
- [Migrating from 2.x](#migrating-from-2x)
- [Migrating from 1.x](#migrating-from-1x)
- [API reference](#api-reference)
- [Development](#development)
- [Project structure](PROJECT_STRUCTURE.md)
- [Resumo em português](#resumo-em-português)
- [License](#license)

---

## What the SDK does

Aeko exposes two independent entry points into the same agent graph:

| Entry point | Class | Use it for |
| --- | --- | --- |
| **Conversation** | `AekoMessenger` | The ESG chatbot: a user message in, a reviewed answer out, against the session document you pass in. |
| **Inventory report** | `AekoInventoryAnalyzer` | A GHG inventory in, a full improvement plan out. |

Both share the same agents, the same registered tools and the same configuration. The
difference is where the run enters the graph and how it ends.

## The agent system

Nine agents, each with its own prompt, persona and tools. Names are in Portuguese
because they are also the routing keys the graph and `set_tools()` use — pass them
exactly as written, accents included.

| Agent | Role | Model |
| --- | --- | --- |
| `Roteador` | First-touch triage; classifies intent and hands off. | fast |
| `FAQ` | Answers institutional/conceptual questions directly. | fast |
| `Orquestrador` | Consolidates the specialists' output and replies to the user. | fast |
| `Guardrail de Saída` | Reviews the consolidated answer before it can leave. | fast |
| `Verificador de Resposta` | Last word of the chat flow: compares what was asked with what was generated. | fast |
| `Análista de inventários` | Reads the GHG inventory itself. | slow |
| `Analista de Poluentes` | Pollutant analysis. | slow |
| `Analista de Gases Verdes` | Green gas alternatives. | slow |
| `Coordenador de Melhoria Contínua` | Writes the improvement plan. Terminal in the report flow; a finding for the orchestrator in a chat. | slow |

The five cheap agents (classify, consolidate, review) run on `fast_model`; the four
specialist analysts run on `slow_model`. Both are configurable, and each model is
registered with the other as its fallback — a provider hiccup degrades a run rather than
failing it, which is also why a turn can report a model you did not ask for.

Agents hand off to each other by ending their answer with a literal `Next agent: <name>`
line, which the graph reads and the SDK strips before the text reaches you. It is
protocol between agents, never part of an answer.

**Conversational flow** — enters at the router; a consolidated answer passes the
guardrail and then the response checker:

```
send_message()
      │
      ▼
  Roteador ──────────────────────────▶ FAQ ─────────────────────────▶ answer
      │
      ├──▶ Analista de Poluentes ─────┐
      ├──▶ Analista de Gases Verdes ──┼──▶ Orquestrador ──▶ Guardrail de Saída
      │                               │                            │
      └──▶ Coordenador de Melhoria ───┘               approved? ──▶ Verificador de Resposta
                                                          │                     │
                                                   rejected (2x)          approved? ──▶ answer
                                                          │                     │
                                                          ▼              rejected (2x)
                                                      Roteador ◀─────────────────┘
```

Only the FAQ answers the user directly. Every other agent of this flow — the coordinator
of improvement plans included — produces a **finding**, which the Orquestrador turns into
the answer. That is why the plan's `## Problema definido` / `## Método` / `## Raciocínio`
headings never reach a chat: they are the shape of a document the report flow persists,
not of a reply.

The two reviewers ask different questions of the same draft. The **guardrail** is a
policy filter: it reads only the draft and the question, and asks whether the text may
reach the user at all (domain, internal leaks, injected instructions, sensitive data,
tone). Code fences and internal agent names are rejected before its model is called. The
**response checker** asks whether the draft answers what was actually asked, in full, and
contradicts nothing in the analyses, the question or recognized references — it is the
last thing standing between a hallucination and the user, which is why nothing is
delivered until it approves.

Each of them can send the answer back to the router **twice**. The second rejection ends
the run with **no** answer at all, and `send_message()` raises `MalformedAgentOutputError`
rather than returning an empty turn — the run's `aeko_metrics` travels on the exception.

**Inventory flow** — enters at the inventory analyst, ends at a terminal node, so it
passes neither reviewer:

```
analyze()
      │
      ▼
  Análista de inventários ──▶ Analista de Poluentes ⇄ Analista de Gases Verdes
                                                              │
                                                              ▼
                                        Coordenador de Melhoria Contínua ──▶ plan
```

A run that entered here is **confined** to those analysts: an agent naming any other
successor — or naming itself — is routed to the coordinator instead. Without that, one
stray handoff would kill the run after every analyst had already been paid for.

## Requirements

- **Python 3.11** (`>=3.11,<3.12`)
- A **Google Gemini API key**

Runtime dependencies (`langchain-core`, `pydantic`, `langchain-classic`, `langgraph`,
`langchain-google-genai`) are installed with the package.

## Installation

```bash
pip install aeko
```

Distribution and import name are the same: `aeko`. (The GitHub repository is `aeko-sdk`.)

## Quickstart

```python
from aeko import (
    Aeko,
    AekoInventoryAnalyzer,
    AekoMessenger,
    AekoSession,
    AekoUser,
    AekoUserMemory,
)

# 1. Configure the SDK once, for the whole process.
Aeko.config("YOUR_GEMINI_API_KEY")

# 2. Chat. The documents come straight out of your database, on every request.
user = AekoUser.model_validate(db.user.find_one({"_id": user_id}))
session = AekoSession.model_validate(db.session.find_one({"_id": session_id}))
memories = [
    AekoUserMemory.model_validate(document)
    for document in db.user_memory.find({"id_user": user_id})
]

messenger = AekoMessenger(user, memories)
reply = messenger.send_message(
    "What is the difference between scope 1 and scope 2?",
    session,
    id_request="req-8a31",  # yours, so you can correlate what the run cost
)
print(reply.message.output)  # the answer
print(reply.agents_called)  # e.g. ['FAQ']
print(reply.approved)  # guardrail verdict
print(reply.aeko_metrics.latency)  # e.g. 2841 (ms)

# `reply.message` is one entry of `session.messages`, ready to append.
db.session.update_one(
    {"_id": session_id}, {"$push": {"messages": reply.message.model_dump()}}
)

# 3. Analyze an inventory.
analyzer = AekoInventoryAnalyzer()
analyzer.set_context(
    "Last report: 12,400 tCO2e, scope 1 dominated by the boiler fleet."
)

analysis = analyzer.analyze(
    inventory_markdown,
    id_external_inventory=502,
    id_request="req-8a32",
    gases=[{"id": 1, "name": "CO2"}],
    scopes=[{"id": 1, "name": "Scope 1"}],
    categories=[{"id": 1, "name": "Stationary combustion", "classification": None}],
)
db.improvement_plan.insert_one(analysis.plan.model_dump(by_alias=True, exclude={"id"}))
db.aeko_metrics.insert_one(analysis.aeko_metrics.model_dump())
# `analysis.inventory` is the Postgres payload (description, period, emission/reduction lines).
```

---

## Integrating into other systems

### Design rules that shape the integration

Three deliberate design choices decide how you wire Aeko into your backend. Read these
before writing the integration — they explain every recommendation below.

1. **The SDK never reads the environment.** There is no `GEMINI_API_KEY` fallback, no
   `.env` loading. Your application owns its configuration and passes it in through
   `Aeko.config()`. Nothing works until it does.
2. **Configuration and tools are process-wide.** `Aeko.config()` and
   `AekoMessenger.set_tools()` write to a single process-wide runtime, and *any* write to
   it drops the agents so the next run rebuilds them. Call them **at startup**, not per
   request. (`set_tools` is a `classmethod` for exactly this reason: registering tools on
   one instance would silently rebuild the agents behind every other instance's back.)
3. **The SDK keeps no session state at all.** The conversation lives in the `AekoSession`
   you pass to every `send_message()` call, and the SDK updates that object in place
   before returning. Nothing is cached between calls, so any worker can serve any
   conversation — and the process never accumulates sessions it cannot evict.

### 1. Configure once, at startup

```python
from aeko import Aeko

Aeko.config(
    api_key=settings.GEMINI_API_KEY,
    fast_model="gemini-3.1-flash-lite",  # router, FAQ, orchestrator, both reviewers
    slow_model="gemini-3.5-flash",  # the four specialist analysts
    max_tokens=1024,  # output cap for chat turns
    report_max_tokens=8192,  # output cap for the inventory report
    temperature=1.0,  # sampling temperature
    top_p=0.95,  # nucleus sampling probability
    top_k=64,  # candidate sampling limit
)
```

Every keyword argument is optional and falls back to the default shown. Calling
`Aeko.config()` again applies the change immediately — the agents and the compiled graph
are rebuilt on the next run. `Aeko.is_configured()` tells you whether a key was supplied
(useful in a health check); `Aeko.reset()` restores every default and clears registered
tools.

### 2. Register your tools

Tools are **your** application's code — a price lookup, an emission-factor table, a search
over your own documents. Aeko binds them to the agents you choose.

```python
from langchain_core.tools import tool
from aeko import AekoMessenger, AekoTool


@tool
def consulta_precos(equipamento: str) -> str:
    """Return the average market price for a piece of equipment."""
    return price_service.lookup(equipamento)


AekoMessenger.set_tools(
    {
        "Coordenador de Melhoria Contínua": [
            AekoTool(
                tool=consulta_precos,
                description="Use to price the equipment you recommend, in BRL.",
            ),
        ],
        # A bare LangChain tool works too — its own docstring becomes the description.
        "FAQ": [buscar_norma],
    }
)
```

Each tool is used **twice, from a single declaration**: its description is rendered into
that agent's `# Ferramentas Disponiveis` prompt section, and the tool object itself is
bound to that agent's executor. The prompt therefore can never advertise a tool the agent
is unable to call.

Keys must be agent names from the table above; anything else raises `UnknownAgentError`,
which carries the valid names in `.known_agents`. `AGENT_NAMES` is exported for
validating input before you get there:

```python
from aeko import AGENT_NAMES  # ('Roteador', 'FAQ', 'Orquestrador', ...)
```

`set_tools()` replaces the whole registry, so pass every agent's tools in one call.
Appending to an already-registered list is the one write the runtime cannot observe, and
the agents would go on using the tools they were built with.

### 3. The conversational flow

```python
from aeko import AekoMessage, AekoMessenger, AekoSession, AekoUser, AekoUserMemory

# Built once per user — it holds nothing about any conversation.
messenger = AekoMessenger(
    AekoUser(
        id_external_user=1001,
        role="ESG analyst",
        usecase="Tracks the boiler fleet's gas substitution.",
    ),
    [AekoUserMemory(field="preferred_language", description="Answers in Portuguese")],
)

# Rehydrated per request, from what you persisted.
session = AekoSession(
    id="64b8f0a1c9e1a2b3c4d5e6f3",
    id_user="64b8f0a1c9e1a2b3c4d5e6f1",
    name="Scope 1 review",
    messages=[AekoMessage(input="What is scope 3?", output="Scope 3 covers...")],
)

response = messenger.send_message(
    "Our scope 1 jumped 12% this quarter. Where do I look?",
    session,
    id_request="req-8a31",
)
```

`id_request` is **required and keyword-only**. It is yours: whatever your API correlates
this request by — a trace id, a job id, the HTTP request id. The SDK reads no database and
invents nothing, so it cannot derive one; it echoes yours back on the returned
[event tracking](#5-event-tracking). It never reaches a prompt and is never logged.

The two documents your database already holds go in as they are, so there is no separate
"history" format to translate: `session.messages` **is** the conversation, replayed to the
agents oldest first. Nothing is kept between calls — pass the session on every request and
any worker can serve it.

**Only the 10 most recent turns are replayed** (`SESSION_HISTORY_USAGE`, in
`aeko.config.messenger`). Send the session as long as it is: a conversation of 500 turns
costs a run exactly what
one of 10 does, and you do not have to slice anything on the way in. What the SDK caps is
what the agents *read*, never what you persist — the turn it answers is appended to the
session you passed, and every earlier turn is still there when the call returns.

`user.role` and `user.usecase` become the business context every agent reads. The
identifiers (`_id`, `id_external_user`, `id_user`) never reach a prompt: they are there so
you can correlate documents, and a model can do nothing with them.

`send_message()` returns an `AekoMessageResponse`:

| Field | Meaning |
| --- | --- |
| `message` | The turn, as one entry of `session.messages` — ready to append. |
| `id_session` / `id_user` | The `_id`s of the session and user this answer belongs to, echoed back from the session you sent in. |
| `agents_called` | Names of the agents that contributed, in call order. |
| `approved` | Whether the output guardrail approved the answer. Always `True` on a returned response — an answer no reviewer approved raises instead. |
| `guardrail_retries` | How many times the guardrail sent the draft back. |
| `aeko_metrics` | What the request cost and went through — see [Event tracking](#5-event-tracking). |

Only `message` belongs in the `session.messages` array; everything else describes *how*
the run reached the answer, or which documents it belongs to, and is deliberately not part
of the persisted entry — `session.messages[]` carries no identifiers of its own. The identifiers
are still returned so a response is enough on its own to file and log the answer against
the right conversation. The turn itself mirrors the collection exactly:

| Field | Meaning |
| --- | --- |
| `input` | What the user sent. |
| `output` | The final user-facing text, already stripped of the agents' internal routing markers. Never empty: a turn no reviewer approved raises instead of returning. |
| `submitted_at` | When the turn was answered (UTC). |

**What the turn cost is not on the turn.** The model that served it and the tokens it
burned live on `aeko_metrics.used_agents`, per agent invocation — a finer account of the
same thing (see [Event tracking](#5-event-tracking)). A rolled-up copy here would be a
second record of one fact, free to drift from the first and impossible to tell apart once
it had. To answer "what did this conversation cost", sum the invocations:

```python
sum(agent.input_tokens for agent in response.aeko_metrics.used_agents)
```

An answered turn is appended to the `AekoSession` you passed in, and `updated_at` is
bumped — the same object, updated in place, so you can persist exactly what you handed
over. **Only a final result is recorded**: a turn a reviewer rejected raises and
is *not* appended, because a draft that never reached the user cannot become context for
the next question.

**User memories.** Hand the `user_memory` documents to the constructor and **every one of
them** is rendered into the same business context the user's role and usecase go into, so
every agent of the run reads them:

```python
messenger = AekoMessenger(user, memories)  # list[AekoUserMemory]
```

Each memory becomes one `"- <field>: <description>"` line (`AekoUserMemory.to_prompt_line()`)
under a `Memórias do usuário:` heading, with `expires_at` and the identifiers left out — a
model can act on what was remembered, not on when the row stops being valid. There is no
cap here, unlike the conversation: a memory is already the condensed form of something the
user told you once, and dropping any of them would silently un-remember it.

**Deciding which memories are still valid is your API's job**: filter `expires_at` before
handing the list over. A user with no memories is normal — the argument is optional, and
no empty section reaches the prompt.

### 4. The inventory flow

```python
from aeko import AekoInventoryAnalyzer

analyzer = AekoInventoryAnalyzer()

# Optional: a company's first report legitimately has no previous one.
analyzer.set_context(
    "2025 report: 12,400 tCO2e, scope 1 dominated by the boiler fleet."
)

analysis = analyzer.analyze(
    inventory_markdown,
    id_external_inventory=502,
    id_request="req-8a32",
    gases=gases,
    scopes=scopes,
    categories=categories,
)
plan = analysis.plan
inventory = analysis.inventory
```

`analyze()` expects the inventory **rendered as Markdown** — a table is the natural shape.
It also requires the three auxiliary catalogs of that deployment, keyword-only:
`gases`, `scopes` and `categories`. Each gas/scope item is `{id, name}`; each category
item is `{id, name, classification}` where classification is `UPSTREAM`, `DOWNSTREAM`
or `null`. The SDK never reads Postgres, so those ids cannot be derived here. It runs
with `report_max_tokens` instead of the chat cap, since this flow writes a full
report that the chat-sized cap would truncate. `id_external_inventory` is what ties the
resulting plan back to the inventory; neither it nor `id_request` can be derived here —
both are required and keyword-only.

It returns an `AekoAnalysisResponse`, an envelope of three things: `plan`, the Mongo
document to persist; `inventory`, the structured payload aligned with Postgres
(`description`/`start_period`/`end_period` plus `emissions` lines); and `aeko_metrics`,
what producing them cost (see [Event tracking](#5-event-tracking)). The metrics stay
out of `plan` because the `improvement_plan` collection has no column for a latency.
`inventory` is not a Mongo document: each line with `is_reduction=false` is an
`emission` row, and `is_reduction=true` is a `reduction` row, using the catalog ids
from the same call as foreign keys.

`analysis.plan` is an `AekoImprovementPlan`, mirroring one document of the collection:

| Field | Meaning |
| --- | --- |
| `id` | The document's `_id`. Always `None` on a fresh plan — the database owns it. |
| `id_external_inventory` | The inventory the plan was produced from. |
| `defined_problem` | The problem the analysis identified. |
| `method` | What to do about it. |
| `reasoning` | Why that method addresses that problem. |
| `recommended_gases` | Up to 2 recommended gas substitutions (`AekoRecommendedGas`), `[]` when none applies — a valid outcome, never a failure. Defaults to `[]` for plans built by hand. |
| `updated_at` | When the plan was produced (UTC). |

The continuous improvement coordinator is instructed — in its scope, its tasks and every
one of its few-shot examples — to write exactly those plan fields under four fixed
headings, and the SDK reads them straight back out:

```text
## Problema definido
...

## Método
...

## Gases recomendados
[]

## Raciocínio
...
```

The `## Gases recomendados` body is the only one read as JSON: an array of 0 to 2 items,
each `{"scope": 1|2|3, "emission": tCO2e >= 0, "reduction_percentage": 0..100, "name": "..."}`.
Both floats are rounded to 2 decimals. A missing section, invalid JSON, more than 2 items or
an out-of-bounds value is sent back like a missing section; `[]` is accepted as is.

Headings rather than a JSON object, for the same reason the agents route on a literal
`Next agent:` line: this flow runs with the report token cap, and a truncated JSON object
is unparseable and costs the whole plan, while a truncated last section still yields the
ones written before it. That truncation argument applies to the **plan in sections**, not
to `inventory`: on the happy path the coordinator still writes a separate fenced
```inventory JSON block after the four headings. The graph does **not** use structured
output for the whole answer — only free-form prose plus that fence. Only those four
headings delimit a plan section, so a subtitle the agent writes mid-answer never cuts its
own text short — and case and accents are ignored when matching them, so `## RACIOCINIO`
is read as `## Raciocínio`.

An answer that misses a plan section is **sent back to the coordinator to be rewritten**,
with the missing headings named and the original request attached, up to four times
(`PLAN_FORMAT_MAX_RETRIES`). That retry happens **inside the graph** and only re-asks the
coordinator — the inventory, pollutant and green gas analysts already ran, and their
findings stay in the state. If the format never comes, `analyze()` raises
`MalformedAgentOutputError` rather than padding the plan with guesses. The model is never
given a say in `_id` or `updated_at` — only the content fields are read back from it.

**After the graph**, the SDK parses the plan first, then recovers `inventory` in a
deterministic step that does not re-run the analysts:

1. **Salvage** — locate the ```inventory fence in the coordinator's answer, parse the JSON,
   and tolerate common slips (trailing commas, Python `None`/`True`/`False`). If that
   fence is missing, a single JSON object after `## Raciocínio` is accepted the same way.
   Recovered JSON is stripped from the plan text so it cannot leak into `reasoning`.
2. **Canonicalize** — normalize types and nulls and derive `is_upstream` from each line's
   category classification in the catalogs you passed in.

If salvage yields a dict, canonicalization and contract validation run locally. Failures
that are **semantic** — an unknown catalog id, a quantity out of range, a shape the
persistence contract rejects — raise `MalformedAgentOutputError` and **do not** ask the
model again.

When salvage cannot recover JSON at all, the SDK may call the **same slow coordinator**
again with a constrained inventory-only prompt, up to two times
(`INVENTORY_SCHEMA_MAX_RETRIES`). The plan parsed from the first answer stays **frozen**;
these calls only try to produce the structured payload. Each attempt is an extra
`Coordenador de Melhoria Contínua` entry in `aeko_metrics.used_agents`. If salvage and
every schema attempt fail, `analyze()` raises `MalformedAgentOutputError` — but that is
not the same as failing on the first malformed fence with no recovery attempt.

**Chat is unchanged.** `send_message()` never asks for an ```inventory fence and never
runs salvage or schema fill; the coordinator's three headings stay report-only.

**Cost.** A well-formed report costs one coordinator call at the report token cap, on top
of the analysts. A plan that uses every in-graph rewrite costs five coordinator calls for
the prose, same as before. Schema fill is **additional**: zero on the happy path when
salvage succeeds, and at most two slow coordinator calls when JSON is irrecoverable from
the first answer.

There is no `approved` field: this flow ends at the continuous improvement coordinator, a
terminal node, and never reaches the output guardrail or the response checker. Its answer
is a document to be parsed, not a message to a user, so a reviewer sitting at the end of
it would be judging the wrong thing.

### 5. Event tracking

Both entry points hand back an `AekoMetrics` describing the request that just ran —
`response.aeko_metrics` on a chat turn, `analysis.aeko_metrics` on an analysis. It exists
because the SDK writes to no database: what it observes about a run leaves with the run's
answer or is lost when the process moves on.

| Field | Meaning |
| --- | --- |
| `id_request` | The `id_request` you passed in, echoed back. |
| `latency` | How long the whole request took, in whole milliseconds. |
| `error_description` | Why it failed, or `None`. Filled from the exception when a request raised — including a turn neither reviewer approved. |
| `flow` | `"conversational"` for `send_message()`, `"analytical"` for `analyze()`. |
| `used_agents` | One `AekoAgentMetrics` per agent **invocation**, in call order. |

Each entry of `used_agents`:

| Field | Meaning |
| --- | --- |
| `name` | The agent, under the exact name the graph routes it by. |
| `input_tokens` / `output_tokens` | What that single invocation consumed, the whole tool-calling loop included. |
| `llm` | The model that served it. Comma-separated if the cross-model fallback fired mid-call. |
| `used_tools` | The tools it **actually called**, in call order — not the ones registered for it. |

**One entry per call, not per agent.** The reviewers' retry loops run the same agents
again and again, and a turn that paid for three routings is not a turn that paid for one:

```python
tracking = response.aeko_metrics

[agent.name for agent in tracking.used_agents]
# ['Roteador', 'Analista de Poluentes', 'Orquestrador', 'Guardrail de Saída',
#  'Verificador de Resposta', 'Roteador', 'Orquestrador', ...]

sum(agent.input_tokens for agent in tracking.used_agents)
# 176 — what the whole turn cost, review retries included
```

A turn both reviewers kept sending back can reach ~12 entries. Budget the document size
accordingly.

**A failed request still reports.** `analyze()` raises `MalformedAgentOutputError` when
the coordinator never produces the plan's three sections, and there is no response left to
carry the metrics — so they are attached to the exception instead:

```python
try:
    analysis = analyzer.analyze(
        inventory,
        id_external_inventory=502,
        id_request=rid,
        gases=gases,
        scopes=scopes,
        categories=categories,
    )
except AekoError as exc:
    if exc.aeko_metrics:  # None for errors raised outside a request
        db.aeko_metrics.insert_one(exc.aeko_metrics.model_dump())
    raise
```

`AekoError.aeko_metrics` is `None` for an error raised outside any request — a refused
`Aeko.config()`, or tools registered for an agent that does not exist.

**This does not replace the logs.** The SDK still writes one
`[aeko-sdk] [module] [datetime] ...` line per request to its own `aeko` logger, and the
event tracking is built from the same observations rather than from a second pass — the
agents one lists are the agents the other lists, in the same order.

### 6. Full example: a stateless FastAPI service

This is the shape Aeko was designed for: an HTTP API with more than one worker, where
conversation history lives in **your** database and is handed back to the SDK on every
request.

```python
from contextlib import asynccontextmanager

from fastapi import FastAPI, HTTPException
from pydantic import BaseModel

from aeko import (
    Aeko,
    AekoError,
    AekoInventoryAnalyzer,
    AekoMessenger,
    AekoNotConfiguredError,
    AekoSession,
    AekoTool,
    AekoUser,
    AekoUserMemory,
    MalformedAgentOutputError,
)

from .settings import settings
from .tools import buscar_norma, consulta_precos


@asynccontextmanager
async def lifespan(app: FastAPI):
    # Process-wide setup: exactly once, before the first request.
    Aeko.config(settings.GEMINI_API_KEY, report_max_tokens=12288)
    AekoMessenger.set_tools(
        {
            "FAQ": [buscar_norma],
            "Coordenador de Melhoria Contínua": [
                AekoTool(
                    tool=consulta_precos,
                    description="Price the equipment you recommend.",
                ),
            ],
        }
    )
    yield


app = FastAPI(lifespan=lifespan)


class ChatRequest(BaseModel):
    session_id: str
    message: str
    request_id: str


@app.post("/chat")
def chat(body: ChatRequest):
    # 1. Read the documents — this worker may never have seen this session before.
    #    The DTOs take them exactly as they come out of the collections.
    session = AekoSession.model_validate(db.session.find_one({"_id": body.session_id}))
    user = AekoUser.model_validate(db.user.find_one({"_id": session.id_user}))

    #    Expiring the memories is yours to do: the SDK renders every one it gets.
    memories = [
        AekoUserMemory.model_validate(document)
        for document in db.user_memory.find({"id_user": user.id, "expires_at": None})
    ]

    messenger = AekoMessenger(user, memories)

    # 2. Run it. Blocking call: keep it off the event loop in production
    #    (`run_in_threadpool`, a task queue, or a sync worker).
    try:
        response = messenger.send_message(
            body.message, session, id_request=body.request_id
        )
    except AekoNotConfiguredError as exc:
        raise HTTPException(status_code=503, detail=str(exc)) from exc
    except MalformedAgentOutputError as exc:
        # 3. Neither reviewer approved a draft: there is no answer to persist,
        #    but the run still has to be accounted for — its metrics come on
        #    the exception, since there is no response to carry them.
        if exc.aeko_metrics:
            db.aeko_metrics.insert_one(exc.aeko_metrics.model_dump())
        raise HTTPException(
            status_code=502,
            detail="No answer passed review. Please rephrase.",
        ) from exc

    # 4. Persist what the run cost.
    db.aeko_metrics.insert_one(response.aeko_metrics.model_dump())

    # 5. Persist the turn so the next request (on any worker) can replay it.
    db.session.update_one(
        {"_id": response.id_session},
        {
            "$push": {"messages": response.message.model_dump()},
            "$set": {"updated_at": session.updated_at},
        },
    )

    return {
        "answer": response.message.output,
        "agents": response.agents_called,
        "approved": response.approved,
        "guardrail_retries": response.guardrail_retries,
    }


class InventoryRequest(BaseModel):
    inventory_markdown: str
    id_external_inventory: int
    request_id: str
    previous_report: str | None = None
    gases: list[dict]
    scopes: list[dict]
    categories: list[dict]


@app.post("/inventory")
def inventory(body: InventoryRequest):
    analyzer = AekoInventoryAnalyzer()

    if body.previous_report:
        analyzer.set_context(body.previous_report)

    try:
        analysis = analyzer.analyze(
            body.inventory_markdown,
            id_external_inventory=body.id_external_inventory,
            id_request=body.request_id,
            gases=body.gases,
            scopes=body.scopes,
            categories=body.categories,
        )
    except AekoError as exc:
        # A failed analysis has no response to carry its metrics — the exception does.
        if exc.aeko_metrics:
            db.aeko_metrics.insert_one(exc.aeko_metrics.model_dump())
        raise HTTPException(status_code=502, detail=str(exc)) from exc

    db.aeko_metrics.insert_one(analysis.aeko_metrics.model_dump())

    # `exclude={"id"}`: the plan is new, so let MongoDB generate its `_id`.
    db.improvement_plan.insert_one(
        analysis.plan.model_dump(by_alias=True, exclude={"id"})
    )

    return analysis.plan.model_dump(exclude={"id"})
```

Notes for production:

- **`send_message()` and `analyze()` are synchronous and slow** (several model calls per
  run, more when the guardrail retries). Don't block an async event loop with them — use
  a threadpool, a background worker, or a task queue.
- **Never call `Aeko.config()` or `set_tools()` per request.** Both rebuild every agent
  process-wide; doing it under load throws away warm agents for every concurrent run.
- **The SDK holds no session state.** Read the `AekoSession` document and pass it to
  `send_message()` on every request; any worker can then serve any conversation.
- Configuration and registered tools are shared mutable process state, so treat startup as
  the only place that writes them.

### 7. Error handling

Every error the SDK raises inherits from `AekoError`, so one `except` covers the SDK:

| Exception | Raised when | Typical response |
| --- | --- | --- |
| `AekoNotConfiguredError` | `Aeko.config()` was never called, or the key is empty/not a string. | `503` — a deployment problem, not a user one. |
| `UnknownAgentError` | `set_tools()` got a key that is not an agent name. Carries `.agent` and `.known_agents`. | Fail at startup. |
| `MalformedAgentOutputError` | A run produced nothing usable: the plan never came back in its four headings after four in-graph rewrites; salvage and up to two inventory schema fills could not yield a valid structured payload; semantic inventory validation failed after a dict was obtained; or a chat turn was rejected by the output guardrail or the response checker up to their retry caps. | `502` — retry rather than persisting a guess or an empty turn. |
| `AekoError` | Base class for all of the above. | Catch-all. |

Every `AekoError` raised *inside* a request carries that request's
[event tracking](#5-event-tracking) on `.aeko_metrics`, so a run that failed can still be
persisted. It is `None` for errors raised outside one — `AekoNotConfiguredError` from a
refused `Aeko.config()`, or `UnknownAgentError` from `set_tools()`.

```python
from aeko import AekoError

try:
    response = messenger.send_message(text, session, id_request=request_id)
except AekoError as exc:
    logger.exception("Aeko failed")
    if exc.aeko_metrics:
        db.aeko_metrics.insert_one(exc.aeko_metrics.model_dump())
    raise HTTPException(status_code=500, detail=str(exc)) from exc
```

A *rejected* answer is an exception since 3.2: a turn neither reviewer approved raises
`MalformedAgentOutputError` instead of returning an empty `message.output`. The run is
still fully accounted for — its event tracking comes on `exc.aeko_metrics`, with the
reason in `error_description`.

---

## Migrating from 2.x

Two signatures changed, and nothing else. There is no shim: a 2.x call fails immediately
with a `TypeError`, which is the point — an event tracking nobody can correlate is not
worth producing.

```python
# 2.x
reply = messenger.send_message(text, session)
plan = analyzer.analyze(markdown, id_external_inventory=502)

# 3.1
reply = messenger.send_message(text, session, id_request=request_id)
plan = analyzer.analyze(markdown, id_external_inventory=502, id_request=request_id).plan
```

| What | Change |
| --- | --- |
| `send_message()` | Add `id_request=` — required, keyword-only. Returns the same `AekoMessageResponse`, now with an `aeko_metrics` field. |
| `analyze()` | Add `id_request=`. **Returns `AekoAnalysisResponse`, not `AekoImprovementPlan`** — read the plan off `.plan`. |
| `AekoMessageResponse(...)` built by hand | `aeko_metrics` is a required field. |
| `AekoMessage` | Lost `llm`, `input_tokens` and `output_tokens`. Read the cost off `aeko_metrics.used_agents` instead — and stop writing those three to `session.messages`, or ignore them on the documents you already have. |
| Nothing else | The DTOs, the agent names, the tools, the graph and the logs are untouched. |

If you build an `AekoMessageResponse` yourself (tests, fixtures), the smallest valid
tracking is `AekoMetrics(id_request="...", flow="conversational")`.

---

## Migrating from 1.x

Every name below changed in 2.0. There is no deprecation shim: the 1.x calls fail at
import or call time rather than quietly doing something else.

Every 1.x DTO was a plain dataclass describing a *call*. Every 2.0 DTO is a Pydantic model
mirroring a *collection*, which is what makes the hand-off to the database lossless.

| 1.x | 2.0 |
| --- | --- |
| `SessionInfo(session_id, user_info, turns)` | `AekoSession` — the `session` document itself, `messages` included |
| `MessageResponse(session_id, answer, ...)` | `AekoMessageResponse` — `.answer` becomes `.message.output`, a full `AekoMessage` |
| `InventoryAnalysisResponse(answer, agents_called, context_used)` | `AekoImprovementPlan` — three named fields instead of one `answer` blob |
| — | `AekoUser`, `AekoUserMemory`, `AekoMessage` are new |
| `SessionNotPreparedError` | removed — there is nothing to prepare |
| — | `MalformedAgentOutputError` is new |
| `messenger.prepare(session_id, user_info, history) -> SessionInfo` | removed — construct with `AekoMessenger(user, memories)` |
| `send_message(text) -> MessageResponse` | `send_message(text, session) -> AekoMessageResponse` |
| `analyze(inventory) -> InventoryAnalysisResponse` | `analyze(inventory, *, id_external_inventory) -> AekoImprovementPlan` |

**The shape of the change.** In 1.x the messenger owned a process-wide dictionary of
sessions, and you announced a conversation to it before using it. In 2.0 the conversation
is a document you already have:

```python
# 1.x
messenger = AekoMessenger()
messenger.prepare(session_id, user_info="ESG analyst", history=[...])
result = messenger.send_message("Our scope 1 jumped 12%.")
answer = result.answer

# 2.0
messenger = AekoMessenger(user, memories)  # who is asking
response = messenger.send_message("Our scope 1 jumped 12%.", session)
answer = response.message.output  # may be "" — check it
```

Four consequences worth planning for:

1. **You must persist the session.** The SDK caches nothing, so the turn appended to
   `session.messages` is lost unless you write it back. In exchange, any worker can serve
   any conversation.
2. **`user_info` is now structured.** The free-form string that went into `prepare()` is
   `AekoUser.role` plus `AekoUser.usecase`, and anything else you were packing into it
   probably belongs in `AekoUserMemory` — which is rendered in a section of its own.
3. **An answer can be refused.** `.answer` was always populated; since 3.2 a turn that
   neither the output guardrail nor the response checker approved raises
   `MalformedAgentOutputError` instead of returning, so `.message.output` is never empty
   on a response you actually receive.
4. **The improvement plan is structured.** `analyze()` returns `defined_problem`, `method`
   and `reasoning` instead of one `answer` string, and raises `MalformedAgentOutputError`
   when the coordinator never produces them — code that used to split the report apart
   should read the fields directly. `context_used` is gone; you know whether you called
   `set_context()`.

## API reference

Everything below is importable directly from `aeko`.

**`Aeko`** — configuration facade.

| Member | Signature |
| --- | --- |
| `config` | `config(api_key: str, *, fast_model: str \| None = None, slow_model: str \| None = None, max_tokens: int \| None = None, report_max_tokens: int \| None = None, temperature: float \| None = None, top_p: float \| None = None, top_k: int \| None = None) -> None` |
| `is_configured` | `is_configured() -> bool` |
| `reset` | `reset() -> None` |

**`AekoMessenger`** — conversational entry point.

| Member | Signature |
| --- | --- |
| `set_tools` *(classmethod)* | `set_tools(tools: dict[str, list[AekoTool \| Any]]) -> None` |
| *constructor* | `AekoMessenger(user: AekoUser, memories: Sequence[AekoUserMemory] \| None = None)` |
| `send_message` | `send_message(message: str, session: AekoSession, *, id_request: str) -> AekoMessageResponse` |

**`AekoInventoryAnalyzer`** — report entry point.

| Member | Signature |
| --- | --- |
| *constructor* | `AekoInventoryAnalyzer()` |
| `set_context` | `set_context(context: str) -> None` |
| `analyze` | `analyze(inventory: str, *, id_external_inventory: int, id_request: str, gases: list, scopes: list, categories: list) -> AekoAnalysisResponse` |

**Data objects.** Every DTO that crosses the API boundary is a Pydantic model mirroring one
MongoDB collection, field for field:

| DTO | Collection |
| --- | --- |
| `AekoUser` | `user` |
| `AekoUserMemory` | `user_memory` |
| `AekoSession` | `session` |
| `AekoMessage` | `session.messages[]` |
| `AekoImprovementPlan` | `improvement_plan` |
| `AekoRecommendedGas` | `improvement_plan.recommended_gases[]` |

`model_validate(document)` takes a raw document and `model_dump(by_alias=True)` gives one
back — `_id` included, under that exact name — so the hand-off is lossless in both
directions.

`AekoMessageResponse` (the turn plus the run's metadata), `AekoAnalysisResponse` (the plan,
the structured `inventory` for Postgres, and the run's metadata), `AekoExtractedInventory`,
`AekoInventoryEmission`, `AekoCatalogItem`, `AekoCategoryCatalogItem`, `AekoInventoryCatalogs`,
`AekoMetrics`, `AekoAgentMetrics` and `AekoTool` are SDK-only and mirror no Mongo collection.
The catalog and extracted-inventory models mirror the Postgres contract of a call, not a
collection the SDK writes. The two `*Metrics` models are plain Pydantic models — persist them
wherever your telemetry lives; the SDK has no opinion on the collection's name.

The identifiers (`_id`, `id_external_user`, `id_user`, `id_external_inventory`) and
`expires_at` are carried across the boundary in both directions and echoed back on
`AekoMessageResponse` — they are how you correlate and log what came out of a run. What they
never do is reach a prompt: a model can act on a role or a usecase, not on an ObjectId.

**Timestamps.** The SDK stamps a timestamp only on a document it produces itself, and
never invents one for a document it merely received:

| Field | Filled by | Why |
| --- | --- | --- |
| `AekoMessage.submitted_at` | SDK | The turn is created here; only the SDK knows when. |
| `AekoImprovementPlan.updated_at` | SDK | The plan is produced here. |
| `AekoSession.updated_at` | SDK | The SDK is what updates the session, by appending the turn. |
| `AekoSession.created_at` | **You** | The session already exists by the time it reaches `send_message()`. |
| `AekoUserMemory.created_at` | **You** | The memory was recorded by your API; the SDK only reads it into the prompt. |

So `AekoSession`, `AekoUser` and `AekoUserMemory` default their timestamps to `None` rather than to
"now": a session read from the database brings its own `created_at` through untouched, and
one built in Python without it would otherwise be handed a creation date that is simply
false. The consequence to watch for is on the way back — a *new* session built in Python
and dumped straight into MongoDB writes `created_at: null`. Set it at insert time, or drop
the empty fields:

```python
session.model_dump(by_alias=True, exclude_none=True)
```

**Exceptions**: `AekoError`, `AekoNotConfiguredError`, `MalformedAgentOutputError`,
`UnknownAgentError`. Every one raised inside a request carries that request's
`.aeko_metrics`.
**Constants**: `AGENT_NAMES`, `__version__`.

**Defaults and limits**

| Setting | Default | Where it lives |
| --- | --- | --- |
| `fast_model` | `gemini-3.1-flash-lite` | `Aeko.config()` |
| `slow_model` | `gemini-3.5-flash` | `Aeko.config()` |
| `max_tokens` | `1024` | `Aeko.config()` |
| `report_max_tokens` | `8192` | `Aeko.config()` |
| `temperature` | `1.0` | `Aeko.config()` |
| `top_p` | `0.95` | `Aeko.config()` |
| `top_k` | `64` | `Aeko.config()` |
| Guardrail retry cap | `2` | fixed — `GUARD_RAIL_MAX_RETRIES` |
| Response check retry cap | `2` | fixed — `RESPONSE_CHECK_MAX_RETRIES` |
| Plan rewrite cap | `4` | fixed — `PLAN_FORMAT_MAX_RETRIES` |
| Inventory schema fill cap | `2` | fixed — `INVENTORY_SCHEMA_MAX_RETRIES` |
| Replayed turns | `10` | fixed — `SESSION_HISTORY_USAGE` |

## Development

```bash
python -m venv .venv && source .venv/bin/activate   # Windows: .venv\Scripts\activate
pip install -r requirements-dev.txt
pip install -e .
pytest
```

Python **3.11** only. The test suite runs against a scripted fake chat model and never
calls Gemini, so no API key is needed to run it. CI (`.github/workflows/ci.yml`) is that
same `pytest` on 3.11.

How the package is layered, what is public, and the practices a change must follow —
docstrings on every function, no `#` comments, no import cycles from engine into
config, inventory salvage before schema fill, the `Next agent:` handoff — are in
**[PROJECT_STRUCTURE.md](PROJECT_STRUCTURE.md)**. `tests/test_code_quality.py` enforces
the docstring / no-comment rule on `aeko/` and `tests/`.

`requirements.txt` pins the exact versions the suite is green against; `pyproject.toml`
declares the same dependencies as ranges, so installing the SDK next to other LangChain
packages does not deadlock a consumer's resolver.

Releases are published to PyPI by the GitHub Actions workflow in
`.github/workflows/publish.yml`, triggered when a GitHub Release is published. The release
tag must match `aeko.__version__` or the workflow fails before building.

---

## Resumo em português

**aeko** é o núcleo inteligente do ecossistema Aether: um sistema multiagente
(LangChain + LangGraph) que lê o inventário de gases de efeito estufa de uma empresa,
explica os números e devolve um plano de melhoria priorizado — além do chatbot que
responde às dúvidas de ESG do dia a dia. É uma **biblioteca**, não um serviço: qualquer
backend Python pode embuti-la.

```bash
pip install aeko
```

```python
from aeko import (
    Aeko,
    AekoInventoryAnalyzer,
    AekoMessenger,
    AekoSession,
    AekoUser,
    AekoUserMemory,
)

Aeko.config("SUA_CHAVE_GEMINI")  # obrigatório: o SDK não lê variáveis de ambiente

usuario = AekoUser.model_validate(db.user.find_one({"_id": id_usuario}))
sessao = AekoSession.model_validate(db.session.find_one({"_id": id_sessao}))
memorias = [
    AekoUserMemory.model_validate(documento)
    for documento in db.user_memory.find({"id_user": id_usuario})
]

messenger = AekoMessenger(usuario, memorias)
resposta = messenger.send_message(
    "Como reduzo o escopo 1 da nossa caldeira?", sessao, id_request="req-8a31"
)
print(resposta.message.output)
db.session.update_one(
    {"_id": id_sessao}, {"$push": {"messages": resposta.message.model_dump()}}
)
db.aeko_metrics.insert_one(resposta.aeko_metrics.model_dump())

analisador = AekoInventoryAnalyzer()
analise = analisador.analyze(
    inventario_em_markdown,
    id_external_inventory=502,
    id_request="req-8a32",
    gases=gases,
    scopes=scopes,
    categories=categories,
)
db.improvement_plan.insert_one(analise.plan.model_dump(by_alias=True, exclude={"id"}))
db.aeko_metrics.insert_one(analise.aeko_metrics.model_dump())
```

### O que mudou na 3.1

A 3.1 faz o SDK **relatar o que cada requisição custou**. Os dois métodos passaram a exigir
um `id_request` (obrigatório e keyword-only) e devolvem um `AekoMetrics` junto da resposta:
latência em milissegundos, descrição do erro quando houve, o fluxo (`conversational` ou
`analytical`) e uma entrada por **chamada** de agente, com tokens de entrada e saída,
modelo usado e as tools que aquele agente realmente chamou.

Três mudanças quebram compatibilidade: a assinatura dos dois métodos; `analyze()`, que
agora devolve `AekoAnalysisResponse` (`.plan` + `.aeko_metrics`) em vez de
`AekoImprovementPlan`; e o `AekoMessage`, que ficou só com `input`, `output` e
`submitted_at` — `llm`, `input_tokens` e `output_tokens` saíram, porque a `AekoMetrics` já
conta a mesma coisa por chamada de agente, e dois registros do mesmo fato divergem. Quando o run falha e levanta exceção, as métricas vão anexadas nela
(`exc.aeko_metrics`), porque uma requisição que falhou é justamente a que mais interessa
persistir. Os logs continuam iguais — isto acrescenta uma segunda leitura do que o run já
observava, não substitui o stream que você lê no terminal.

### O que mudou na 2.0

A fronteira entre o SDK e a API que o consome foi refeita, e **toda mudança quebra
compatibilidade**: `SessionInfo` e `InventoryAnalysisResponse` saíram, `prepare()` e
`SessionNotPreparedError` sumiram, `send_message()` passou a receber a sessão e devolver
`AekoMessageResponse`, e `analyze()` devolve `AekoImprovementPlan`. O mapa completo de
nomes antigos para novos está em [Migrating from 1.x](#migrating-from-1x).

### Cinco pontos que definem a integração

1. **Configure no startup, nunca por requisição.** `Aeko.config()` e
   `AekoMessenger.set_tools()` alteram um runtime único do processo e reconstroem todos os
   agentes.
2. **As DTOs espelham as collections.** `AekoUser`, `AekoUserMemory`, `AekoSession`,
   `AekoMessage` e `AekoImprovementPlan` são modelos Pydantic com os mesmos campos dos
   documentos, `_id` incluso: `model_validate(documento)` entra e
   `model_dump(by_alias=True)` volta igual. O SDK não fala com o banco — quem lê e grava
   é a API.
3. **O SDK não guarda sessão.** Passe o documento `AekoSession` em todo `send_message()`:
   a conversa é o próprio `session.messages`, o SDK o atualiza in-place e qualquer worker
   atende qualquer sessão. Só os **10 turnos mais recentes** são reenviados aos agentes —
   mande a sessão inteira, o corte é do que o modelo lê, nunca do que você persiste. Já as
   memórias do usuário vão no construtor (`AekoMessenger(user, memories)`) e são
   renderizadas **todas**, sem corte, no contexto que todo agente lê — filtrar as
   expiradas é com a API.
4. **Toda resposta carrega métricas.** `resposta.aeko_metrics` e
   `analise.aeko_metrics` trazem latência, erro, fluxo e o custo por chamada de agente —
   persista onde sua telemetria vive. Numa exceção, as métricas vêm em `exc.aeko_metrics`.
5. **Resposta reprovada levanta exceção.** O fluxo conversacional passa por dois
   revisores: o `Guardrail de Saída`, um filtro de política que decide se o texto pode
   chegar ao usuário (domínio, vazamentos internos, injeção, dados sensíveis e tom), e o
   `Verificador de Resposta`, que compara o que foi pedido com o que foi gerado e barra
   o que contradiz as análises, a pergunta ou referências reconhecidas. Cada um pode devolver a resposta ao
   Roteador **2 vezes**; na segunda reprovação o run acaba sem resposta e o
   `send_message()` levanta `MalformedAgentOutputError` — as métricas vêm em
   `exc.aeko_metrics`.    Um plano fora do formato levanta a mesma exceção depois de até quatro reescritas
   **no grafo**. O inventário estruturado passa por salvage/canonicalização determinística
   após o grafo; só JSON irrecuperável dispara até duas chamadas extras do Coordenador
   (só inventário, plano congelado). Falhas semânticas de catálogo não reconvocam o modelo.
   O chat não usa fence de inventário nem schema fill. JSON recuperado (fence ou objeto
   após Raciocínio) é removido do texto do plano para não vazar em `reasoning`.

Quem for **alterar** o SDK deve seguir [PROJECT_STRUCTURE.md](PROJECT_STRUCTURE.md)
(camadas, API pública, docstrings, testes sem Gemini, protocolo `Next agent:`).

Os nomes dos agentes (`Roteador`, `FAQ`, `Orquestrador`, `Guardrail de Saída`,
`Verificador de Resposta`, `Análista de inventários`, `Analista de Poluentes`,
`Analista de Gases Verdes`, `Coordenador de Melhoria Contínua`) são também as chaves de
roteamento — use-os
exatamente como escritos, inclusive acentuação. A seção em inglês acima tem o detalhamento
completo, incluindo um exemplo de serviço FastAPI stateless.

## License

MIT — see [LICENSE](LICENSE).
