Anwendungsfall für die reale Welt: Schätzung des CO2-Fußabdrucks mithilfe von KI (!)
TL;DR für nicht-technische Leser
We built a smart, interactive system to quickly estimate the carbon footprint of chemicals. Instead of filling out complicated forms, you can just type questions naturally (like asking the CO₂ emissions of shipping chemicals). The system uses advanced AI to understand your request, gathers the needed data (even chatting with you if necessary), and calculates accurate results transparently. This approach blends human-friendly interactions with reliable numbers, ensuring clear and trustworthy estimates.
Versuchen Sie es selbst: Dieses System befindet sich derzeit in der Entwicklung. Sie können den Prototyp live testen unter https://agents.lyfx.ai. Rechnen Sie mit ein paar Ecken und Kanten, während wir den Arbeitsablauf weiter ausbauen und verfeinern.
Das Problem I have long learned that the gap between “we need to calculate something” and “we have a system that actually works” is often filled with more complexity than anyone initially expects. When we set out to build a quick first-order estimator for cradle-to-gate greenhouse gas emissions of any chemical, I thought: “How hard can it be? it is just a simple spreadsheet, right?”
Well, it turns out that when you want users to input free-form requests like “what is the CO₂ footprint of 50 tonnes of acetone shipped 200 km?” instead of filling out rigid forms, you need something smarter than a spreadsheet. Enter: agentic workflows.
Ich habe mit verschiedenen Ansätzen zur Erstellung von Agenten-Workflows experimentiert (von benutzerdefinierten Orchestrierungsschichten bis hin zu anderen Frameworks), aber LangGraph hat sich als die robusteste Lösung für diese Art von hybrider Mensch-KI-Interaktion erwiesen. Es handhabt Zustandsmanagement, Unterbrechungen und komplexe Routing-Muster mit der Art von Zuverlässigkeit, die man braucht, wenn man etwas baut, das die Menschen tatsächlich benutzen werden.
Die Architektur: Orchestriertes Chaos
Unser System geht von einem vereinfachten Lebenszyklus aus: Produktion an einem einzigen Standort, Transport zum Verwendungsort und teilweise oder vollständige Freisetzung in die Atmosphäre. Die Magie liegt jedoch in der Art und Weise, wie wir die komplizierte Interaktion zwischen Mensch und KI handhaben, die zur Erfassung der erforderlichen Parameter erforderlich ist.
Here’s what we built using LangGraph als unsere Orchestrierungs-Engine, eingewickelt in eine Django Anwendung serviert über uvicorn und nginx:
Der Triage-Router-Agent
This is the conductor of our little orchestra. It uses OpenAI’s GPT-4o with structured outputs (Pydantic models, because type safety matters even in the age of LLMs) to classify incoming requests:
class TriageRouter(BaseModel):
reasoning: str = Field(description="Step-by-step reasoning behind the classification.")
classification: Literal["gather information", "calculate", "respond and conclude"]
response: str = Field(description="Response to user's request")
Der Triage-Agent entscheidet, ob wir weitere Informationen benötigen, bereit sind zu rechnen oder eine Antwort geben können. Er ist im Wesentlichen ein Zustandsautomat mit einem LLM-Gehirn.
Der Informationssammler-Agent
An dieser Stelle wird es interessant. Der Agent ist ein Hybrid. Er kann entweder programmatisch Tools aufrufen oder zu einem interaktiven Chat-Agenten weiterleiten, wenn er menschliche Eingaben benötigt. Die verfügbaren Tools sind:
GWP-Datenbank-Tool: Instead of maintaining a static lookup table, we built an LLM-powered “database” that searches through our chemical inventory with over 200 entries. When you ask for methane’s GWP-100, it does not just do string matching; it understands that “CH₄” and “methane” refer to the same molecule. The tool returns a classification (found/ambiguous/not available) plus the actual GWP value.
CO₂ Price Checker: Currently a placeholder returning 0.5 €/ton (we are building incrementally!), but designed to be swapped with a real-time API.
Interaktive Chat-Fähigkeit: When the information gatherer cannot get what it needs from tools, it seamlessly hands off to a chat agent. This is not just a simple handoff: we use LangGraph’s NodeInterrupt mechanism to pause the workflow, collect user input, then resume exactly where we left off.
Der Rechenknecht
Dies ist der einzige Agent, der wirklich rechnet, und zwar ganz bewusst. Er ist ein ReAct-Agent, der mit zwei Rechenwerkzeugen ausgestattet ist:
@tool
def chemicals_emission_calculator(
chemical_name: str,
annual_volume_ton: float,
production_footprint_per_ton: float,
transportation: list[dict],
release_to_atmosphere_ton_p_a: float,
gwp_100: float
) -> tuple[str, float]:
The transportation parameter accepts a list of logistics steps: [{‘step’:’production to warehouse’, ‘distance_km’:50, ‘mode’:’road’}, {‘step’:’warehouse to port’, ‘distance_km’:250, ‘mode’:’rail’}]. Each mode has hardcoded emission factors (road: 0.00014, rail: 0.000015, ship: 0.000136, air: 0.0005 ton CO₂e per ton·km) sourced from EEA data.
Die Rechnung ist bewusst einfach: Produktionsemissionen, Transportemissionen und die Auswirkungen auf die Atmosphäre, die jeweils deterministisch berechnet werden, werden addiert.
Der technische Stapel: LangGraph + Django
Staatliches Management: We use LangGraph’s StateGraph with a custom State class to maintain conversation context, collected data, and routing information across agent handoffs. For development, we use MemorySaver for in-memory persistence. For production with multiple uvicorn workers, we will switch to SqliteSaver with disk-based checkpoints.
Strukturierte Ausgaben: Every agent uses Pydantic models for its responses. This gives us type safety and prevents the usual LLM hallucination issues around routing decisions. When the triage agent says “calculate”, it will always be exactly that string, not “Calculate” or “time to calculate” or other variations.
Unterbrechung und Wiederaufnahme: Der Chat-Agent verwendet NodeInterrupt, um den Arbeitsablauf zu unterbrechen, wenn Benutzereingaben erforderlich sind. Der Status umfasst die Verfolgung, welcher Agent den Chat aufgerufen hat (caller_node), damit wir nach dem Sammeln von Informationen korrekt zurückleiten können.
Django-Integration: Der Workflow läuft innerhalb einer Django-Anwendung, so dass wir bei Bedarf Benutzerauthentifizierung, Datenpersistenz und API-Endpunkte hinzufügen können. Die Bereitstellung erfolgt über uvicorn für asynchrone Unterstützung und nginx für Produktionsstabilität.
Was noch fehlt (in Arbeit)
Datenbank zum Fußabdruck der Produktion: Currently, when the system needs the GHG footprint per ton of a chemical’s production, it asks the user. This is temporary. We are building a database of production routes and their associated emissions. Think of it as a more sophisticated version of what SimaPro or GaBi provides, but focused on chemicals and accessible via API.
Daten in Echtzeit: CO₂ prices, shipping routes, even chemical properties could be pulled from live APIs. But we are building the orchestration layer first, then swapping in real data sources.
Gesprächsspeicher: Jede Berechnung beginnt neu. Das Hinzufügen von Sitzungspersistenz, um frühere Abfragen zu speichern und auf ihnen aufzubauen, ist mit unserer aktuellen Architektur einfach.
Warum diese Architektur funktioniert (und warum Sie sie vielleicht klauen sollten)
Trennung der Belange: LLMs übernehmen die unübersichtliche menschliche Interaktion und das Routing. Python-Funktionen übernehmen die deterministischen Berechnungen. Fachleute können die Berechnungen validieren und ändern, ohne den KI-Stack zu berühren.
Fehlersuchbare Workflows: LangGraph’s state management means you can inspect exactly what each agent decided and why. When something goes wrong, you’re not debugging a black box.
Inkrementelle Komplexität: Beginnen Sie mit fest kodierten Werten, fügen Sie Datenbankabfragen hinzu und integrieren Sie dann Echtzeit-APIs. Die Struktur des Arbeitsablaufs bleibt die gleiche.
Prüfbare Ergebnisse: Every calculation step is logged and traceable. When someone asks “where did that 142.7 tons CO₂e come from?”, you can show them the exact inputs and formula used.
Das größere Bild
This is not just about carbon footprints. The pattern – use LLMs for natural language understanding and workflow orchestration, but keep the critical calculations in deterministic code – applies to any domain where you need to mix soft reasoning with hard numbers.
Supply chain risk assessment? Same pattern. Financial modeling with regulatory compliance? Same pattern. Any time you find yourself thinking “we need a smart interface to our existing calculations,” this architecture gives you a starting point.
Die Zukunft liegt in der hybriden Intelligenz, nicht darin, alles auf einen LLM zu werfen und auf das Beste zu hoffen.
Built in Python with LangGraph, OpenAI APIs, Django. Co-programmed using Claude Sonnet 3.7 and 4, Chat GPT o3 (not “vibe coded”). Currently in active development. Try it at https://agents.lyfx.ai.
