Skip to main content

p2d-kimi

Unofficial Python client for Kimi AI (kimi.moonshot.cn). No API key needed — just your bearer token.

PyPI Version Python Versions License Telegram


Features

  • No API key required — uses your browser session token
  • Persistent sessions — messages stay in the same conversation automatically
  • Streaming support — yield reply chunks in real time
  • Web search — toggle Kimi's built-in search on any message
  • Multi-turn conversations — full context preserved across calls
  • Lightweight — only depends on requests

Install

pip install p2d-kimi

Quick Start

from p2dkimi import KimiClient

client = KimiClient(token="your-kimi-bearer-token")

reply = client.chat("What is the capital of France?")
print(reply)

Multi-Turn Conversation

Messages are automatically grouped into the same session. The client creates one session on the first call and reuses it for all subsequent calls.

client = KimiClient(token="your-token")

reply1 = client.chat("My name is Alex.")
reply2 = client.chat("What is my name?")
print(reply2)

Starting a New Session

Call new_chat() to explicitly start a fresh conversation.

client.new_chat(name="fresh-start")

reply = client.chat("Let's start over.")

Streaming

from p2dkimi import KimiClient

client = KimiClient(token="your-token")

for chunk in client.stream("Explain quantum computing in simple terms"):
    print(chunk, end="", flush=True)

With Web Search

reply = client.chat("Latest news on AI today", use_search=True)

Manual Chat ID

You can still pass a chat_id directly to target a specific conversation.

chat_id = client.new_chat(name="my-session")

reply1 = client.chat("My name is Alex.", chat_id=chat_id)
reply2 = client.chat("What is my name?", chat_id=chat_id)
print(reply2)

API Reference

KimiClient(token, model="kimi", timeout=60)

Parameter Type Description
token str Bearer JWT from kimi.moonshot.cn
model str "kimi" (default), "k1", "ok-computer", etc.
timeout int HTTP timeout in seconds

Methods

Method Returns Description
chat(message, chat_id, history, use_search) str Send a message, get full reply
stream(message, chat_id, history, use_search) Generator[str] Send a message, yield reply chunks
new_chat(name) str Create a new conversation and set it as active

Getting Your Token

  1. Open kimi.moonshot.cn in your browser
  2. Open DevTools → Network → any request
  3. Copy the value of Authorization: Bearer <token>

Or from iOS/Android using a MITM proxy (e.g. mitmproxy, Charles).

The token is a JWT that lasts ~30 days.


How It Works

KimiClient.chat("hello")
    → new_chat()          POST /api/chat            → chat_id (stored in self.chat_id)
    → stream_completion() POST /api/chat/{id}/completion/stream  → SSE chunks
    → collect chunks      event=cmpl text="Hello!"
    → return full string

KimiClient.chat("follow-up")
    → reuses self.chat_id (no new session created)
    → stream_completion() POST /api/chat/{id}/completion/stream  → SSE chunks

REST API Server

The included Flask server exposes HTTP endpoints:

Endpoint Method Description
POST /chat/new POST Create a new session → returns chat_id
POST /chat POST Send a message → returns reply + chat_id
POST /chat/stream POST Stream a reply as plain text
GET /health GET Health check

Pass your token via Authorization: Bearer <token> header. Include chat_id in the body to continue an existing session.


Demo

See DEMO.md for a full working Telegram bot built with this library.


Community

Telegram Channel

Join for Python tutorials, projects, and updates → t.me/pythontodayz


License

MIT — made by addy

Release files for p2d-kimi 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for p2d-kimi 0.1.1
File Size Uploaded
p2d_kimi-0.1.1.tar.gz 5.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for p2d-kimi 0.1.1
File Interpreter ABI Platform
p2d_kimi-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 11.7 kB

Release files / p2d_kimi-0.1.1.tar.gz

Download URL p2d_kimi-0.1.1.tar.gz
Size 5.3 kB
Tags Source
SHA-256 checksum
How to use checksums
6023cc9424e001d89c9965e649b9a44e9e5fc811285a7946d51445e60cda94cb
BLAKE2b-256 checksum
How to use checksums
6879a5de5285ee3647af448bf9330640a548cf92310e2b5f0248cbf614ea4c4b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 23, 2026.

Transparency log

Release files / p2d_kimi-0.1.1-py3-none-any.whl

Download URL p2d_kimi-0.1.1-py3-none-any.whl
Size 6.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
835b1b777fe9fb977ec66db2fb07d929eb243bddadd75405ed977a963a22ade0
BLAKE2b-256 checksum
How to use checksums
27d8502555ed872020445e3b27fc2f205756f6bdf1edcd123c1a729dfaf817f2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 23, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page