Skip to main content
Kimi K3 provides reasoning, coding, and tool-calling capabilities for complex tasks. This guide uses an industry-research agent to show how to combine an official web-search tool with one custom tool in a runnable, bounded agent.

Break Down the Task

Split industry research into three stages before choosing tools and writing prompts:
  1. Retrieve: define the research scope and find current data, company information, and news;
  2. Analyze: compare sources, identify conflicts, and separate facts, estimates, and inferences;
  3. Deliver: produce a structured report with a summary, key findings, risks, and sources.
This split lets the model plan and make judgments while tools handle retrieval or deterministic operations. Avoid repeating tool behavior in a long system prompt.

Design Tools

This example combines two kinds of tools:
  • Official web-search: retrieves current industry sources. The platform also provides fetch, code_runner, excel, and other tools. See Official Tools for the full list and Formula API flow;
  • Custom build_research_plan: generates a deterministic scope from a topic, region, and year range, demonstrating how to declare and execute a local function tool.
Custom tools describe arguments with JSON Schema. Setting additionalProperties to false and listing mandatory fields in required reduces invalid arguments. When your inventory grows to dozens or hundreds of tools, do not send every schema in every request. Follow Kimi K3 Tool Calling Best Practices and Dynamically Loaded Tools to retrieve candidates first and load them on demand.

Design the Prompt

Keep the system prompt focused on the role, workflow, and quality boundaries. Leave argument details in the tool schema:
Add constraints incrementally when the business format, compliance requirements, or audience changes. See Prompt Best Practices for more guidance.

Configure the K3 API

Use Python 3.9 or later, and install the OpenAI Python SDK and the HTTP client used for official Formula tools:
The Global endpoint is https://api.moonshot.ai/v1, and the model is kimi-k3. The API key is read only from the MOONSHOT_API_KEY environment variable. Kimi K3 always reasons, and the only currently supported value for the top-level reasoning_effort field is "max". A tool loop must append the complete assistant message returned by the SDK to messages. Copying only content and tool_calls drops any returned reasoning_content and breaks the context needed by later tool calls. For parameters and model-specific behavior, see Thinking Mode, Thinking Effort, and the Model Parameter Reference.

Complete Agent Loop

Save the following code as agent.py. It loads the web-search declaration dynamically, executes the custom tool locally, and handles tool calls in a loop capped at eight rounds.
Two context details in the loop are mandatory:
  1. messages.append(message) appends the complete assistant message and preserves K3’s reasoning_content;
  2. every tool message uses the matching tool_call_id, so the model can associate each result with its call.
The example fails immediately on finish_reason="length" instead of treating truncated output as a final report. If one tool call fails, its error is returned as the matching tool result while the loop continues processing the other calls in that round. MAX_TOOL_ROUNDS prevents endless tool calls and avoids the growing call stack of a recursive implementation.

Run and Troubleshoot

Run the example:
Replace the question in main() with your target industry, region, and years. The final response should contain a summary, key findings, risks, and sources instead of a dump of raw tool output. Common issues:
  • finish_reason is length: the example raises RuntimeError; increase max_completion_tokens using the Model Parameter Reference, or shorten tool results;
  • Maximum tool rounds reached: check for overlapping tool descriptions and ambiguous results, then narrow the task;
  • A tool repeatedly returns argument errors: check the function schema, required, enum, and additionalProperties; do not replace argument constraints with prompt text;
  • A later tool round fails: confirm that you append the complete SDK assistant message and preserve the correct tool_call_id on every result;
  • An official tool request fails: verify the endpoint, API key, Formula URI, and tool availability. See Official Tools for the full API flow.

Custom Tools and Optimization

The included build_research_plan example covers the complete path: declare a schema, dispatch locally, return JSON, and attach the matching tool_call_id. To connect a database, internal search service, or file generator, replace the function implementation while keeping its schema and return shape aligned. For further optimization: