Mistral Agents API Advanced (Python)
Mistral Agents API Advanced (Python)
Section titled “Mistral Agents API Advanced (Python)”Latest: 2.0.1 | Updated: April 2026 Upstream: https://github.com/mistralai/client-python/releases | https://docs.mistral.ai/getting-started/clients | https://pypi.org/project/mistralai/
BREAKING (v2.0.1): The Mistral SDK v2.0.1 API is NOT backwards-compatible with v1.x. Install with
pip install mistralai>=2.0.0.
Patterns
Section titled “Patterns”- Tool reliability with retries/backoff
- Structured outputs and validation
- Tracing and metrics with OTEL
Tool-Calling Chat (Python SDK)
Section titled “Tool-Calling Chat (Python SDK)”from mistralai.client import Mistralimport os, json
client = Mistral(api_key=os.environ["MISTRAL_API_KEY"])
tools = [ { "type": "function", "function": { "name": "get_weather", "description": "Get weather by city", "parameters": { "type": "object", "properties": {"city": {"type": "string"}}, "required": ["city"], }, }, }]
messages = [ {"role": "user", "content": "What's the weather in Paris?"}]
resp = client.chat.complete( model="mistral-large-latest", messages=messages, tools=tools,)
msg = resp.choices[0].messageif msg.tool_calls: for call in msg.tool_calls: if call.function.name == "get_weather": args = json.loads(call.function.arguments) # call your real weather API here tool_result = {"temp_c": 18, "condition": "cloudy", "city": args["city"]} messages.append(msg) messages.append({ "role": "tool", "tool_call_id": call.id, "name": call.function.name, "content": json.dumps(tool_result), }) final = client.chat.complete(model="mistral-large-latest", messages=messages) print(final.choices[0].message.content)Resilience
Section titled “Resilience”- Wrap downstream API calls (weather, DB) with retries/backoff and timeouts
- Validate tool outputs (Pydantic) before sending back to model
Observability
Section titled “Observability”- Trace each step: user -> tool call -> tool result -> final answer
- Record tokens, model, tool names, and durations
Streaming
Section titled “Streaming”for event in client.chat.stream(model="mistral-large-latest", messages=messages, tools=tools): # event could be tool_call, content_delta, or completed print(event)Deployment (Kubernetes)
Section titled “Deployment (Kubernetes)”deployment.yaml
apiVersion: apps/v1kind: Deploymentmetadata: name: mistral-agentspec: replicas: 2 selector: matchLabels: app: mistral-agent template: metadata: labels: app: mistral-agent spec: containers: - name: app image: ghcr.io/yourorg/mistral-agent:latest env: - name: MISTRAL_API_KEY valueFrom: secretKeyRef: name: mistral-secrets key: apiKey ports: - containerPort: 8080---apiVersion: v1kind: Servicemetadata: name: mistral-agentspec: selector: app: mistral-agent ports: - port: 80 targetPort: 8080CI/CD (GitHub Actions)
Section titled “CI/CD (GitHub Actions)”.github/workflows/deploy.yml
name: deployon: push: branches: [ main ]jobs: docker: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: docker/setup-buildx-action@v3 - uses: docker/login-action@v3 with: registry: ghcr.io username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - uses: docker/build-push-action@v5 with: push: true tags: ghcr.io/${{ github.repository }}:latest deploy: needs: docker runs-on: ubuntu-latest steps: - uses: azure/k8s-deploy@v5 with: action: deploy manifests: | deployment.yaml images: ghcr.io/${{ github.repository }}:latest namespace: defaultSecurity Best Practices
Section titled “Security Best Practices”- Store keys in cloud secret managers (Key Vault/Secret Manager/Secrets Manager)
- Use least-privileged IAM roles; separate read/write roles for tools
- Network egress allowlisting for tool calls
- Encrypt logs; avoid sensitive data in traces
- Rotate keys regularly; monitor for anomalies