Agents & Orchestration

Building a Deep Research Agent powered by Tavily and Token Factory

Wire Tavily web search into a Deep Agent running on a Nebius Token Factory model, then ask it a research question and get back a sourced, multi-step report.

Build it with an agent

Paste into Claude Code, Cursor, or any coding agent — it builds the recipe for you.

Build a small, production-minded Python application that runs a deep research agent. Use the `deepagents` framework with `langchain-nebius` for the model and `langchain-tavily` for web search. Deliver `app.py`, `requirements.txt`, a `Dockerfile`, a `.dockerignore`, pytest tests under `tests/`, and a `README.md`.

Read the Token Factory credential from `NEBIUS_API_KEY`, the API base URL from `NEBIUS_BASE_URL` (default `https://api.tokenfactory.nebius.com/v1`), the model ID from `NEBIUS_MODEL` (default `MiniMaxAI/MiniMax-M3`), and the search credential from `TAVILY_API_KEY`; never print or commit either credential. Configure `TavilySearch` with `max_results=5` and `search_depth="advanced"`, build the agent with `create_deep_agent(model=model, tools=[tavily_search])`, and take the research question from the first CLI argument with a sensible default. The container command `python app.py` must complete one research run, exit zero, and print the final report prefixed exactly with `Deep research report:`. Keep the tests deterministic by stubbing both the model client and the search tool so no test requires a live API call. Document local `.venv` setup, run the tests, then run the application once. Include clear verification, troubleshooting, Docker, and cleanup instructions.

Recipe

A research agent plans, searches, reads, revises, and cites where each claim came from, instead of answering from training data alone. This cookbook wires Tavily web search into a Deep Agent driven by a Nebius Token Factory model, so the answer is grounded in pages fetched at runtime.

What you will build

A research agent that takes a question, decomposes it into sub-questions, runs advanced Tavily searches, and returns a structured report. It has three pieces:

  • MiniMax-M3 on Nebius Token Factory as the reasoning model, reached through langchain-nebius.
  • Tavily Search as the agent’s tool, configured for advanced depth and five results per query.
  • deepagents to supply the planning loop, the virtual file system for notes, and the sub-agents that turn a chat model into a researcher.

A measured run of the default question made 6 model requests, cost about $0.07 at Token Factory pricing, and produced a 20,000-character report in under 90 seconds.

Prerequisites

  • Python 3.12 or newer
  • Nebius Token Factory account with access to MiniMaxAI/MiniMax-M3
  • Tavily account and API key
  • Both credentials in environment variables, never in a notebook cell or a commit

Run the cookbook

  1. Install the dependencies:

    python -m venv .venv && source .venv/bin/activate
    pip install -U deepagents langchain langchain-core langchain-nebius langchain-tavily tavily-python
    
  2. Export both credentials:

    export NEBIUS_API_KEY="your-token-factory-key"
    export TAVILY_API_KEY="your-tavily-key"
    
  3. Build the agent from a search tool, a model, and one call that combines them:

    from deepagents import create_deep_agent
    from langchain_nebius import ChatNebius
    from langchain_tavily import TavilySearch
    
    tavily_search = TavilySearch(max_results=5, search_depth="advanced")
    model = ChatNebius(model="MiniMaxAI/MiniMax-M3")
    agent = create_deep_agent(model=model, tools=[tavily_search])
    
  4. Ask a research question and print the report:

    result = agent.invoke({
        "messages": [
            {
                "role": "user",
                "content": "Research GPUs available in the US in 2026 and write a detailed report.",
            }
        ]
    })
    
    print(result["messages"][-1].content)
    

Everything between the question and the report is the agent deciding what to search for, what to read, and when it has enough to write.

Verify the result

A successful run returns a report with concrete, checkable specifics (named products, dates, figures) and the sources Tavily surfaced. If the output reads like a generic summary with no sources, the search tool was never invoked; check that TAVILY_API_KEY is set in the same environment.

To see the agent’s planning steps and tool calls rather than just the answer, print the whole state instead of the last message:

import json
print(json.dumps(result, indent=2, default=str))

Troubleshooting

  • 401 from Token Factory: the key is missing, expired, or scoped to a different project. Re-export NEBIUS_API_KEY and retry.
  • Empty or missing search results: confirm the Tavily key is active and your plan has remaining credits.
  • The run takes a minute or two: this is expected. A research pass makes several model calls, and advanced search depth is slower than basic.

Clean up

The cookbook creates no cloud resources, so there is nothing to tear down:

deactivate && rm -rf .venv
unset NEBIUS_API_KEY TAVILY_API_KEY

If you ever pasted either key into a notebook, a shell history file, or a commit, rotate it in the Token Factory console and the Tavily dashboard rather than deleting the file and assuming it is gone.