When deploying agents to LangSmith, you can add custom middleware to your server to handle concerns like logging request metrics, injecting or checking headers, and enforcing security policies without modifying core server logic. This works the same way as adding custom routes. You just need to provide your ownDocumentation Index
Fetch the complete documentation index at: https://langchain-5e9cc07a-preview-openap-1765292790-ae6c60d.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
Starlette app (including FastAPI, FastHTML and other compatible apps).
Adding middleware lets you intercept and modify requests and responses globally across your deployment, whether theyâre hitting your custom endpoints or the built-in LangSmith APIs.
Below is an example using FastAPI.
âPython onlyâ
We currently only support custom middleware in Python deployments with
langgraph-api>=0.0.26.Create app
Starting from an existing LangSmith application, add the following middleware code to yourwebapp.py file. If you are starting from scratch, you can create a new app from a template using the CLI.
Configure langgraph.json
Add the following to your langgraph.json configuration file. Make sure the path points to the webapp.py file you created above.
Customize middleware ordering
By default, custom middleware runs before authentication logic. To run custom middleware after authentication, setmiddleware_order to auth_first in your http configuration. (This customization is supported starting with API server v0.4.35 and later.)
Start server
Test the server out locally:X-Custom-Header in its response.