Skip to main content
If you have already built your own agent loop on top of the Model Inference API—maintaining message history, executing tools, and handling retries yourself—this guide shows how to migrate that system to Kimi Hosted Agents so the platform manages the runtime while you keep only the logic unique to your product.
Migration does not require an SDK compatibility layer: Hosted Agents exposes standard HTTP + SSE endpoints that you can call with curl or any HTTP client. The examples below read your API key from the KIMI_API_KEY environment variable.

What changes

With a self-built harness, your application code owns every part of the runtime. After migrating, those runtime responsibilities move to the platform: In short: hand “how it runs” to the platform, and keep “what task to solve” in your configuration and business code.

Migration steps

1

Move system prompts, tool definitions, and skills into an Agent configuration

Consolidate the system prompts, tool schemas, and other instructions previously hardcoded in your loop into a persistent Agent configuration object. Create an agent once and reference it by ID across sessions; each update creates a new immutable version.
Here is how components from your loop map to Hosted Agents:
  • System prompt → the agent’s system field.
  • Business tools executed on your client → declare them as custom tools so your client continues executing them; to let the platform connect directly to third-party services, use MCP.
  • Hand-written utility tools (search, file I/O, shell commands) → agents get built-in cloud sandbox tools (file read/write, code execution, web search, and more) by default—no declaration needed—so you can delete your own implementations.
  • Structured domain playbooks (prompts + scripts + templates) → package and attach them as skills.
For full agent configuration fields and versioning behavior, see Create and Manage Agents.
2

Delete your custom agent loop and message history storage

In this step, you can remove the following boilerplate harness components entirely:
  • The main orchestration loop (“call model → parse tool calls → execute tools → append results → call model again”).
  • Assembling and truncating message history, and maintaining persistent message storage.
  • Fallback logic for crash recovery, stream reconnection, and timeout retries.
Context compaction, history management, and interruption recovery are handled by the platform—you no longer resend the full conversation history on each request.
3

Switch to creating a session, subscribing to the event stream, and sending events

Replace single-turn Model Inference API calls with a three-step flow: create a session, subscribe to its event stream, and send events. Below, AGENT_ID is the id returned in Step 1, SESSION_ID is the id returned when creating the session, and ENVIRONMENT_ID is the id returned when you create an execution environment:
Subscribe to the event stream before sending the message so you do not miss events emitted right after submission. If the connection drops, reconnect with the Last-Event-ID header set to the last received SSE message id to resume from where the stream left off; see Event Stream.
For session creation, status transitions, and lifecycle details, see Create a Session.

What you do not need to migrate

The platform provides the capabilities below out of the box, so you do not need to rebuild them in your system:
  • Persistent session history: Event history is persisted server-side and can be fetched in full at any time without maintaining your own message database.
  • Disconnect and failure recovery: Event streams resume from a cursor after network drops, and after a platform failure, execution automatically recovers and resumes from where it left off.
  • Timeouts and retries: Execution-layer retries, scheduling, and context compaction are managed by the platform—your client only needs to handle HTTP error codes for its own requests.
If you want to keep your own agent loop and fine-grained control over every model call, continue using the Model Inference API. Kimi Hosted Agents is built for long-horizon tasks where you want to offload the entire runtime to the platform.

Next steps

Migrate agent configuration

Consolidate system prompts and tool definitions into a versioned Agent configuration.

Start a managed session

Create sessions and send events in place of a self-built agent loop.

Inspect event history

Subscribe to events, resume from cursors, and review all event types.