Break Down the Task
Split industry research into three stages before choosing tools and writing prompts:- Retrieve: define the research scope and find current data, company information, and news;
- Analyze: compare sources, identify conflicts, and separate facts, estimates, and inferences;
- Deliver: produce a structured report with a summary, key findings, risks, and sources.
Design Tools
This example combines two kinds of tools:- Official
web-search: retrieves current industry sources. The platform also providesfetch,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.
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: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: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 its reasoning effort is configured with the top-level reasoning_effort request field, which supports "low" / "high" / "max" (default "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, Reasoning Effort, and the Model Parameter Reference.
Complete Agent Loop
Save the following code asagent.py. It loads the web-search declaration dynamically, executes the custom tool locally, and handles tool calls in a loop capped at eight rounds.
messages.append(message)appends the complete assistant message and preserves K3’sreasoning_content;- every tool message uses the matching
tool_call_id, so the model can associate each result with its call.
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: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_reasonislength: the example raisesRuntimeError; increasemax_completion_tokensusing 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, andadditionalProperties; 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_idon 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 includedbuild_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:
- expose only tools needed for the current task and avoid overlapping tools;
- validate tool inputs in your application and return structured errors so the model can correct arguments;
- use Dynamically Loaded Tools for large inventories;
- before using
tool_choice, reasoning effort, or related controls, check Kimi K3 Tool Calling Best Practices and the Model Parameter Reference.