How can I determine whether a flow run has been executed ad-hoc or was running on schedule?

Prefect 2.0

You can check that using the auto_scheduled flag available in the flow run context. If this flag is True, then a flow run has been triggered via a regular schedule rather than ad-hoc.

import prefect
from prefect import task, flow

def print_task_context():

def main_flow():
    print("Flow run scheduled through a deployment?")

if __name__ == "__main__":

Prefect 1.0

Similarly to Orion, Prefect 1.0 also provides the auto_scheduled flag recorded for each flow run. However, it is not available in the context and needs to be retrieved either through UI or GraphQL API. Example query:

query = """ 
query($id: uuid!) {
  flow_run(where: {id: {_eq: $id} }) {
client.graphql(query=query, variables=dict(id=prefect.context["flow_run_id"]))