Creación de un flujo de trabajo agenético ligero

Caso práctico para el mundo real: Estimación de la huella de carbono mediante IA (¡!)

TL;DR para lectores no técnicos

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.

Pruébelo usted mismo: Este sistema está actualmente en desarrollo activo. Puede probar el prototipo en vivo en https://agents.lyfx.ai. A medida que vayamos desarrollando y perfeccionando el flujo de trabajo, iremos encontrando algunas asperezas.


El problema 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.

He experimentado con varios enfoques para la construcción de flujos de trabajo de agentes (desde capas de orquestación personalizadas a otros marcos), pero LangGraph ha surgido como la solución más robusta para este tipo de interacción híbrida humano-AI. Maneja la gestión de estados, interrupciones y patrones de enrutamiento complejos con el tipo de fiabilidad que se necesita cuando se construye algo que la gente realmente va a utilizar.

La arquitectura: Caos orquestado

Nuestro sistema parte de un ciclo de vida simplificado: producción en un único lugar, transporte hasta el punto de uso y liberación parcial o total en la atmósfera. Pero la magia está en cómo gestionamos la complicada interacción entre el ser humano y la inteligencia artificial necesaria para reunir los parámetros requeridos.

Here’s what we built using LangGraph como nuestro motor de orquestación, envuelto en un Django solicitud servida a través de uvicornio y nginx:

El agente de enrutamiento de triaje

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")

El agente de triaje decide si necesitamos más información, si estamos listos para calcular o si podemos concluir con una respuesta. es esencialmente una máquina de estados con un cerebro LLM.

El agente recopilador de información

Aquí es donde las cosas se ponen interesantes. El agente es híbrido. Puede llamar a herramientas de forma programática o dirigirse a un agente de chat interactivo cuando necesita intervención humana. Las herramientas disponibles son:

Herramienta de base de datos GWP: 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.

Chat interactivo: 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.

El agente calculista

Se trata de un agente ReAct equipado con dos herramientas de cálculo:

@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.

La matemática es intencionadamente sencilla: sumar las emisiones de la producción, las emisiones del transporte y los impactos de la liberación atmosférica, cada uno calculado de forma determinista.

La pila técnica: LangGraph + Django

Gestión estatal: 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.

Resultados estructurados: 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.

Interrupciones y reanudación: El agente de chat utiliza NodeInterrupt para pausar el flujo de trabajo cuando se necesita la entrada del usuario. El estado incluye el seguimiento de qué agente llamó al chat (caller_node) para que podamos enrutar de vuelta correctamente después de recopilar información.

Integración de Django: El flujo de trabajo se ejecuta dentro de una aplicación Django, lo que nos permite añadir autenticación de usuario, persistencia de datos y puntos finales de API según sea necesario. Lo servimos a través de uvicorn para soporte asíncrono y nginx para robustez de producción.

Lo que falta (Trabajo en curso)

Base de datos de la huella de producción: 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.

Datos en tiempo real: 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.

Conversación Memoria: Cada cálculo empieza de cero. Añadir persistencia de sesión para recordar consultas anteriores y construir sobre ellas es sencillo con nuestra arquitectura actual.

Por qué funciona esta arquitectura (y por qué querrías robarla)

Separación de preocupaciones: Los LLM se encargan de la interacción humana y el enrutamiento. Las funciones de Python se encargan de los cálculos deterministas. Los expertos pueden validar y modificar los cálculos sin tocar la pila de IA.

Flujos de trabajo depurables: 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.

Complejidad incremental: Comience con valores codificados, añada búsquedas en bases de datos y, a continuación, integre API en tiempo real. La estructura del flujo de trabajo sigue siendo la misma.

Resultados auditables: 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.

Panorama general

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.

El futuro es la inteligencia híbrida, no simplemente lanzarlo todo en un LLM y esperar lo mejor.

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.

es_ESES