---
layout: default
---

# Middleware Custom

<div class="mt-4 text-xs">

<v-click>

```typescript
import { createAgent, createMiddleware } from "langchain";

const loggingMiddleware = createMiddleware({
  name: "LoggingMiddleware",
  beforeModel: (state) => {
    console.log(`Llamando modelo con ${state.messages.length} mensajes`);
    return;  // No modificar estado
  },
  afterModel: (state) => {
    const lastMsg = state.messages[state.messages.length - 1];
    console.log(`Modelo respondió: ${lastMsg.content?.slice(0, 50)}...`);
    return;
  },
});

const agent = createAgent({
  model: "mistral:mistral-large-latest",
  tools: [weatherTool],
  middleware: [loggingMiddleware],
});
```

</v-click>

</div>
