Skip to main content

Track OpenAI, Claude, Gemini and OpenAI-compatible models then give solutions to improve your agent system.

Project description

mwin

mwin: Track OpenAI, Claude, Gemini and OpenAI-compatible models then give solutions to improve your agent system.
Our goal is to make llm application more valuable and effortlessly improve llm capabilities.

Quickstart

You can use pip install mwin

pip install mwin

OR pip install from source.

git clone https://github.com/yanghui1-arch/mwin.git
cd src
pip install -e .

Then you need to configure mwin through CLI.

mwin configure

Then you just follow the instructions to configure mwin.

> Which deployment type do you choose?
> 1 - mwin Cloud Platform (default)
> 2 - mwin Local Platform
> Please input the choice number.>2
> Please enter your API key:
> What's your project name? mwin-demo
> Congrats to configure mwin.

It needs an Mwin API key. You can get the apikey after logging http://localhost:5173. Finally use @track to track your llm input and output.

The simplest Demo

from mwin import track
from openai import OpenAI

openai_apikey = "<llm_api_key>"
openai_base_url = "<llm_base_url>"
model = "<llm_model>"

@track()
def run_agent(prompt: str):
    cli = OpenAI(base_url=openai_base_url, api_key=openai_apikey)
    content = cli.chat.completions.create(
        messages=[{"role": "user", "content": f"{prompt}"}],
        model=model
    ).choices[0].message.content
    return content

run_agent("hello, mwin.")

Using start_trace() to manually set the trace scope

It's the most recommended method to use mwin to track the trace in a project. mwin offers two context manager to make trace scope more clear. It's very easy to use and not breaking change your current project code. Using start_trace_async() for async context manager. The usage of them is both same.

Demo

from mwin import track, StepType, start_trace
from openai import OpenAI

openai_apikey = "<llm_api_key>"
openai_base_url = "<llm_base_url>"
model = "<llm_model>"

@track(step_type=StepType.TOOL)
def execute_bash(command: str):
    # assume execute bash and get a stdout
    return "<bash_stdout>"

@track()
def run_agent(prompt: str):
    cli = OpenAI(base_url=openai_base_url, api_key=openai_apikey)
    content = cli.chat.completions.create(
        messages=[{"role": "user", "content": f"{prompt}"}],
        model=model
    ).choices[0].message.content
    if "bash" in content:
        res = execute_bash(content)
        return res
    return content

@track()
def query_for_information(stmt: str) -> str:
    ...

with start_trace():
    info = query_for_information("mwin")
    run_agent()

Using mwin with Thread Pools

If you are using with start_trace() or async with start_trace_async() Skip this part.

When your program uses ThreadPoolExecutor, multiprocessing.pool.ThreadPool, or similar thread pools, and you wouldn't like to use start_trace() and start_trace_async() you have to be aware of how mwin traces work with thread reuse.

The correct solution is to use contextvars.copy_context(). Wrap your submitted task with copy_context().run() to give each task an isolated context. The trace is probably record unexpectedly without contextvars.copy_context().

import contextvars
from concurrent.futures import ThreadPoolExecutor

executor = ThreadPoolExecutor(max_workers=10)

# Without fix — trace leaks between tasks on the same thread
executor.submit(run_agent)

# With fix — each task gets a clean, isolated context
ctx = contextvars.copy_context()
executor.submit(ctx.run, run_agent)

ctx.run(run_agent) creates a context sandbox: all ContextVar operations inside it go to ctx, not to the thread's persistent context. When ctx.run() returns, ctx is garbage collected along with its trace.

Example: FastAPI with ThreadPoolExecutor

import asyncio
import contextvars
from concurrent.futures import ThreadPoolExecutor

executor = ThreadPoolExecutor(max_workers=10)

@app.post("/chat")
async def chat_handler(request: Request):
    loop = asyncio.get_running_loop()

    def run_agent():
        agent_step_1()   # @track — creates trace, shares it
        agent_step_2()   # @track — reuses same trace
        agent_step_3()   # @track — reuses same trace

    ctx = contextvars.copy_context()
    loop.run_in_executor(executor, ctx.run, run_agent)

Conditions you have to notice

Scenario Action needed
scripts None
Celery tasks None (auto-handled)
fastapi using async not sync None
threading.Thread None
ThreadPoolExecutor Use copy_context().run()
asyncio.loop.run_in_executor Use copy_context().run()
multiprocessing.pool.ThreadPool Use copy_context().run()
ProcessPoolExecutor None (separate processes)

Development

Mwin project package manager is uv. If you are a beginner uver, please click uv link: uv official link

uv sync
uv .venv/Script/activate

You can watch more detailed debug information by using --log-level=DEBUG or set AT_LOG_LEVEL=DEBUG for Windows or export AT_LOG_LEVEL=DEBUG for Linux and Mac.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mwin-0.2.2.tar.gz (38.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mwin-0.2.2-py3-none-any.whl (47.0 kB view details)

Uploaded Python 3

File details

Details for the file mwin-0.2.2.tar.gz.

File metadata

  • Download URL: mwin-0.2.2.tar.gz
  • Upload date:
  • Size: 38.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.17

File hashes

Hashes for mwin-0.2.2.tar.gz
Algorithm Hash digest
SHA256 71e9c6441f7a54366aad632a08cf7898a8ee864537c3a535449e7c73ae5a1230
MD5 b26fe60a85954c01e2a6a915a1c53d97
BLAKE2b-256 9f13adfd0620033f2b175d6ba45ab937ebb5ee0c8be34458ba77c4aadec916dc

See more details on using hashes here.

File details

Details for the file mwin-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: mwin-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 47.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.17

File hashes

Hashes for mwin-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 7a54ec522d036e4996b59bed9386fb29e735782d4ebc63e5b3beeeff77f0b3e0
MD5 7cdd0c0893b4b6edef3143bc783b0f52
BLAKE2b-256 08c0e528f9fbbad3b0a064506ef140999bcac8b8b8e7c7263f56dba7c4c7d8f3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page