These recipes extend the AI Debugging page, which shows how to route LangChain and LangGraph model calls through MockServer. The same approach works for LlamaIndex and the OpenAI Agents SDK: override the base URL so every LLM call goes to MockServer instead of the real provider, then set up expectations in your tests using the LLM mock builder.

 

LlamaIndex

LlamaIndex uses a global Settings object. Override its llm attribute with a model client whose base URL points at MockServer:

from llama_index.core import Settings
from llama_index.llms.openai import OpenAI

# Route every LLM call through MockServer instead of calling the provider directly.
Settings.llm = OpenAI(
    model="gpt-4o",
    api_base="http://localhost:1080/v1",
    api_key="sk-placeholder",  # MockServer does not validate the key
)

# Any LlamaIndex query or agent call now goes through MockServer.
# Set up your expectations before running the query:
#
#   from your_test import mockserver_client
#   mockserver_client.llm_mock("/v1/chat/completions") \
#       .with_provider("OPENAI") \
#       .responding_with(completion().with_text("The answer is 42.")) \
#       .apply()
#
#   response = index.query("What is the answer?")

Alternatively, set the OPENAI_BASE_URL environment variable before your process starts; LlamaIndex's OpenAI integration picks it up automatically:

OPENAI_BASE_URL=http://localhost:1080/v1 pytest tests/

For Anthropic models (llama_index.llms.anthropic), set api_base="http://localhost:1080" and use Provider.ANTHROPIC in your LLM mock builder expectation.

 

OpenAI Agents SDK

The OpenAI Agents SDK (openai-agents package) exposes set_default_openai_client to replace the underlying AsyncOpenAI instance used by all agents. Point that client at MockServer:

import asyncio
from openai import AsyncOpenAI
from agents import set_default_openai_client, Agent, Runner

# Replace the default OpenAI client with one that calls MockServer.
openai_client = AsyncOpenAI(
    base_url="http://localhost:1080/v1",
    api_key="sk-placeholder",
)
set_default_openai_client(openai_client)

# Every model call made by agents now goes through MockServer.
agent = Agent(name="support-bot", instructions="Answer concisely.", model="gpt-4o")

async def main():
    result = await Runner.run(agent, "What is 2 + 2?")
    print(result.final_output)

asyncio.run(main())

To use the environment variable approach instead, set OPENAI_BASE_URL before the process starts; the SDK reads it through the underlying openai library:

OPENAI_BASE_URL=http://localhost:1080/v1 pytest tests/
 

Frameworks Without a Base-URL Override

Some frameworks or SDKs do not expose a base URL parameter. In that case, configure MockServer as an HTTPS proxy and set the standard proxy environment variables so all outbound traffic flows through it:

HTTPS_PROXY=http://localhost:1080
ALL_PROXY=http://localhost:1080

See AI Traffic Inspection for the full setup, including how to trust MockServer's generated TLS certificate and how to set up forwarding expectations so the proxied requests reach their real destinations (or are intercepted by MockServer mocks).