Kosmobids Public API

Build an agent on the API

Orchestrate discovery, qualification, document analysis, and tender comments as tools in an agent loop.

The Public API is a good fit for an agent because every step is a small, typed HTTP call with an explicit resource ID. This tutorial describes a tool layout and an orchestration loop that stays reliable. The snippets use the api helper from the quickstart.

Tool layout

Tool Endpoint Purpose
list_pipeline GET /pipeline Read what the team already tracks.
search_tenders POST /search Discover opportunities from criteria.
get_tender GET /tenders/:tenderId Hydrate CPV, value, deadline, authority, and source data.
get_run_matches GET /saved-searches/:id/runs/:runId/matches Read scored matches from a repeatable search.
find_look_alikes POST /tenders/:tenderId/look-alike Expand from a known strong tender.
download_documents POST /tenders/:tenderId/documents/download Start the document pipeline.
list_tender_files GET /tenders/:tenderId/files Check whether files exist.
read_document GET /documents/:fileId and /pages Read metadata and cited pages.
analyze_tender POST /tenders/:tenderId/analyze Ask structured questions.
poll_analysis GET /analyses/:workflowId Wait for the analysis result.
post_comment POST /tenders/:tenderId/api-comments Leave a plain-text note on the tender (labelled API).

Give the model the tool schemas, not raw HTTP. Keep companyId out of every tool; the API key supplies it.

Orchestration loop

1. Read the pipeline and the saved-search results.
2. Pick candidate tender IDs.
3. For each candidate, hydrate the tender detail.
4. Apply the qualification rules and drop weak candidates.
5. For the survivors, ensure documents are downloaded and processed: poll `GET /tenders/:tenderId/files` until a row's `documentStatus` is `processed`, then keep that row's `fileId` for the document read tools. Do not infer readiness from `processingStatus`, and do not assume source documents are unavailable — they report `documentStatus` the same way uploads do.
6. Analyze the tender with a small, specific question set.
7. Resolve citations to page text or page images.
8. Optionally leave a plain-text note on the tender, and surface the answer with its citations in your own UI.

A practical rule is to stop after the first failed step that requires a human decision. Do not let the agent silently substitute tender metadata for a document answer.

Agent state

Keep the following in your own run state:

  • savedSearchId, runId, and the run status.
  • workflowId for each analysis.
  • The mapping from question text to questionId.
  • The tender IDs already processed, so a retry does not duplicate comments.
  • The final comment ID and timestamp.

The API does not provide a job or session abstraction; your agent is the orchestrator.

Polling inside an agent

Do not let the model call a poll endpoint in a tight loop. Expose a poll_analysis tool that performs the backoff internally and returns only a final result, or run polling outside the model loop and feed the result back.

async function analysisTool(workflowId: string) {
  let delay = 2_000;
  for (let attempt = 0; attempt < 90; attempt++) {
    const analysis = await api<Analysis>(`/analyses/${workflowId}`);
    if (analysis.isFinalStatus) return analysis;
    await new Promise((resolve) => setTimeout(resolve, delay));
    delay = Math.min(delay * 2, 30_000);
  }
  throw new Error('Analysis timed out');
}

The same pattern applies to run matches and document downloads.

Qualification rules

Encode your commercial rules as deterministic code, not as prompt text:

  • Allowed countries and languages.
  • Minimum and maximum contract value.
  • Minimum days to the deadline.
  • Required CPV prefixes.
  • Existing pipeline membership.
  • Whether documents are required before an answer.

The model can rank and explain, but the rules decide what enters the pipeline.

Citations and trust

  • Always hydrate the tender before reasoning about it.
  • Prefer document analysis over title-only reasoning for requirements, certifications, and penalties.
  • Resolve every citation to a page before presenting it.
  • A 404 from /documents/:fileId means no document exists for that ID — usually an uploaded id was used instead of the row's fileId. A document that is still processing returns 200 with status: "new", so confirm documentStatus is processed before concluding the document is missing.
  • If the analysis fails or the documents are missing, say so. Do not invent an answer.
  • Treat match, recommended, fit, and finalScore as signals, not facts.

Security

  • Use a company-scoped key dedicated to the agent.
  • Keep the key server-side. The agent runtime should not expose it to the model or to users.
  • Do not accept a companyId from the model or from an end user and pass it to the API; the key supplies the company.
  • Log request IDs from the x-ctx-id response header for support.

Cost control

  • Use ad-hoc search for exploration and saved searches for repeatable evaluation.
  • Filter run matches server-side with fit before fetching details.
  • Keep analysis question sets short and specific.
  • Use grade: false for look-alike exploration unless you need the LLM judgement.
  • Reuse analysis results instead of re-running the same questions.