Skip to main content
Commands, package names, and image names on this page come from the open-source project that Mibyan Desktop is built on, and can differ from the Mibyan Desktop installer. For the supported Mibyan install and update path, see Install and update.
Mibyan isn’t just a CLI tool. You can import AIAgent directly and use it programmatically in your own Python scripts, web applications, or automation pipelines. This guide shows you how.

Installation

Clone Mibyan and prepare its source environment through PM. The Bash recipe is:
Run your application with python your_app.py from that activated checkout. For PowerShell preparation or an independent interpreter, see the PM developer workflow. Mibyan does not publish a supported wheel or source distribution for requirements.txt installs.
The same environment variables used by the CLI are required when using Mibyan as a library. At minimum, set OPENROUTER_API_KEY (or OPENAI_API_KEY / ANTHROPIC_API_KEY if using direct provider access).

Basic Usage

The simplest way to use Mibyan is the chat() method — pass a message, get a string back:
chat() handles the full conversation loop internally — tool calls, retries, everything — and returns just the final text response.
Always set quiet_mode=True when embedding Mibyan in your own code. Without it, the agent prints CLI spinners, progress indicators, and other terminal output that will clutter your application’s output.

Full Conversation Control

For more control over the conversation, use run_conversation() directly. It returns a dictionary with the full response, message history, and metadata:
The returned dictionary contains:
  • final_response — The agent’s final text reply
  • messages — The complete message history (system, user, assistant, tool calls)
(The task_id you pass in is stored on the agent instance for VM isolation but isn’t echoed back in the return dict.) You can also pass a custom system message that overrides the ephemeral system prompt for that call:

Configuring Tools

Control which toolsets the agent has access to using enabled_toolsets or disabled_toolsets:
Use enabled_toolsets when you want a minimal, locked-down agent (e.g., only web search for a research bot). Use disabled_toolsets when you want most capabilities but need to restrict specific ones (e.g., no terminal access in a shared environment).

Multi-turn Conversations

Maintain conversation state across multiple turns by passing the message history back in:
The conversation_history parameter accepts the messages list from a previous result. The agent copies it internally, so your original list is never mutated.

Saving Trajectories

Enable trajectory saving to capture conversations in ShareGPT format — useful for generating training data or debugging:
Each conversation is appended as a single JSONL line, making it easy to collect datasets from automated runs.

Custom System Prompts

Use ephemeral_system_prompt to set a custom system prompt that guides the agent’s behavior but is not saved to trajectory files (keeping your training data clean):
This is ideal for building specialized agents — a code reviewer, a documentation writer, a SQL assistant — all using the same underlying tooling.

Batch Processing

For running many prompts in parallel, Mibyan includes batch_runner.py. It manages concurrent AIAgent instances with proper resource isolation:
Each prompt gets its own task_id and isolated environment. If you need custom batch logic, you can build your own using AIAgent directly:
Always create a new AIAgent instance per thread or task. The agent maintains internal state (conversation history, tool sessions, iteration counters) that is not thread-safe to share.

Integration Examples

FastAPI Endpoint

Discord Bot

CI/CD Pipeline Step


Key Constructor Parameters


Important Notes

  • Set skip_context_files=True if you don’t want AGENTS.md files from the working directory loaded into the system prompt.
  • Set skip_memory=True to prevent the agent from reading or writing persistent memory — recommended for stateless API endpoints.
  • The platform parameter (e.g., "discord", "telegram") injects platform-specific formatting hints so the agent adapts its output style.
  • Thread safety: Create one AIAgent per thread or task. Never share an instance across concurrent calls.
  • Resource cleanup: The agent automatically cleans up resources (terminal sessions, browser instances) when a conversation ends. If you’re running in a long-lived process, ensure each conversation completes normally.
  • Iteration limits: The default max_iterations=500 is generous. For simple Q&A use cases, consider lowering it (e.g., max_iterations=10) to prevent runaway tool-calling loops and control costs.