Free lesson

Function calling and server-side dispatch

Function calling and server-side dispatch — OpenAI tools and agents without paid execution. Define a Responses function; Dispatch without eval; Model output cannot expand authority. Practical assignment: Practice: Function calling and server-side dispatch.

Demo edition. The materials are undergoing editorial review.

Topics covered

  • Define a Responses function
  • Dispatch without eval
  • Model output cannot expand authority

Define a Responses function

A Responses function places type:function, name, parameters and strict at the same level. Do not blindly reuse another API's nested wrapper. This schema permits only safety or backup note IDs and rejects additional fields. Declared fields are required; supported null unions can represent optional values when appropriate. Expose neither hidden credentials nor arbitrary paths. A definition describes available functionality but does not execute it: the application dispatcher still controls whether and how proposed arguments reach local code.

// tools.mjs
export const readNoteTool = {
  type: 'function', name: 'read_note',
  description: 'Read one fictional public lab note.', strict: true,
  parameters: {
    type: 'object', properties: { noteId: { type: 'string', enum: ['safety', 'backup'] } },
    required: ['noteId'], additionalProperties: false
  }
};

Dispatch without eval

Add dispatchRead to tools.mjs. Validate name and argument shape before an explicit lookup. Unknown tools never become dynamic JavaScript function lookups. JSON.parse creates a value without making it trusted. Fixtures requesting delete_file, an extra command or ../../key must fail without filesystem or network access. The notes are fictional educational rules rather than proof of a server's actual condition, a user's permissions or a completed backup. Never use eval to turn model output into executable code.

const notes = {
  safety: 'Educational rule: model ports stay private.',
  backup: 'Educational rule: verify recovery on a separate copy.'
};
export function dispatchRead(name, args) {
  if (name !== 'read_note') throw new Error('tool_not_allowed');
  if (!args || Array.isArray(args) || typeof args !== 'object'
      || Object.keys(args).join(',') !== 'noteId'
      || typeof args.noteId !== 'string' || !Object.hasOwn(notes, args.noteId)) throw new Error('invalid_arguments');
  return { noteId: args.noteId, text: notes[args.noteId] };
}

Model output cannot expand authority

Even a prompt saying the user authorized everything cannot change the dispatcher's allowlist. Requests for different notes face the same code-level checks. Adding publish, email or shell requires a new contract and review rather than another branch chosen by the model. Bound returned context as well: read-only does not authorize disclosure of every record. This deliberately small public catalog makes its invariants inspectable manually and through negative tests without inference charges or live account connections.

Practical assignment

Practice: Function calling and server-side dispatch

Continue to learning

Course outline

  1. Architecture and API budget
  2. Function calling and server-side dispatch
  3. Tool loop and call_id
  4. Structured responses and business rules
  5. Approval and side-effect boundaries
  6. Errors, evals and final harness