---
layout: default
---

# Middleware: wrapModelCall

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

<v-click>

Permite interceptar y modificar la llamada al modelo:

```typescript
import { ChatMistralAI } from "@langchain/mistralai";
import { createAgent, createMiddleware } from "langchain";

const basicModel = new ChatMistralAI({ model: "mistral-small-latest" });
const advancedModel = new ChatMistralAI({ model: "mistral-large-latest" });

const dynamicModelSelection = createMiddleware({
  name: "DynamicModelSelection",
  wrapModelCall: (request, handler) => {
    // Elegir modelo según complejidad de la conversación
    const messageCount = request.messages.length;
    const model = messageCount > 10 ? advancedModel : basicModel;

    return handler({ ...request, model });
  },
});

const agent = createAgent({
  model: basicModel,
  tools: [searchTool],
  middleware: [dynamicModelSelection],
});
```

</v-click>

</div>
